浙大ZOJ Practice C语言实现

1001 A + B Problem

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main(){
int a,b;
while(scanf("%d %d",&a,&b) != EOF){
printf("%d\n",a+b);
}
return 0;
}

本题需要注意的是输入和输出格式,输入要的是一对数字中间用空格隔开,输出要求占一行

1002 Fire Net

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<stdio.h>

static char city[16];

static unsigned int amount, n;

int main(){
unsigned int i;
void dfs(unsigned int index, unsigned int count);
scanf("%u", &n);
while(n != 0){
amount = 0;
for(i = 0; i < n; i++) scanf("%s", &city[i*n]);
dfs(0,0);
printf("%u\n",amount);
scanf("%u", &n);
}
return 0;
}

void dfs(unsigned int index, unsigned int count){
int noBlockhouse(unsigned int index);

if(index == n * n){
if(count > amount) amount = count;
count = 0;
return;
};

if(city[index] == '.' && noBlockhouse(index)){

city[index] = 'b';
dfs(index + 1, count + 1);

city[index] = '.';
}
dfs(index + 1, count);
}

int noBlockhouse(unsigned int index){

unsigned int row = index / n;
unsigned int column = index % n;

while(index >= row*n && city[index] != 'X' ){
if(city[index] == 'b') return 0;

if(index == 0) break;
index --;
}
while(row >= 0 && city[row*n+column] != 'X'){
if(city[row*n+column] == 'b') return 0;
if(row == 0) break;
row --;
}
return 1;
}

简单的DFS问题