问题描述:
浙江大学ACM 1002 火力网编程,有位大侠,给出代码,各位能否帮忙dfs图示?
#include <iostream>
using namespace std;
bool canput(char map[5][5], int y, int x){
if(map[y][x] != '.')return 0;
int i;
for(i = y - 1; i >= 0; i--){
if(map[i][x] == 'X')break;
if(map[i][x] == 'F')return 0;
}
for(i = x - 1; i >= 0; i--){
if(map[y][i] == 'X')break;
if(map[y][i] == 'F')return 0;
}
return 1;
}
void dfs(char map[5][5], int n, int depth, int put, int &max){
if(put > max)max = put;
if(depth >= n*n)
return;
dfs(map,n,depth+1,put,max);
int y = depth / n, x = depth % n;
if(canput(map,y,x)){
map[y][x] = 'F';
put++;
dfs(map, n, depth+1, put, max);
map[y][x] = '.';
put--;
}
}
int main(){
char map[5][5];
int i,n,k=0,firenet,store[20];
while(scanf("%d",&n)!=EOF && n){
firenet = 0;
for(i = 0; i < n; i++)scanf("%s",&map[i]);
dfs(map,n,0,0,firenet);
store[k]=firenet;k++;
}
for(i=0;i<k;i++)
printf("%d\n",store[i]);
return 0;
}
问题解答:
这么难的问题,很难有人回答
高手一般没时间闲逛 呵呵