原题链接http://noi.openjudge.cn/ch0205/1818/ 我写的代码如下
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#define maxn 50 //最大值为20,加一防止RE
using namespace std;
int x,y; //注意: x为列数,y为行数
int start_x,start_y; //开始时的坐标,输入里不会提供具体值
int sum = 0; //计数器,初始不为一因为后面会变符号
char a[maxn][maxn];
void dfs(int x, int y){
// cout << y << ',' << x << endl;
a[y][x] = '$'; //$符号表示是黑色但是走过了
if(a[y][x+1] == '.'){
// _show();
// cout << y << ',' << x << endl;
dfs(x+1,y);//递归
}
if(a[y][x-1] == '.'){
// cout << y << ',' << x << endl;
// _show();
dfs(x-1,y);//递归
}
if(a[y+1][x] == '.'){
// cout << y << ',' << x << endl;
// _show();
dfs(x,y+1);//递归
}
if(a[y-1][x] == '.'){
// cout << y << ',' << x << endl;
// _show();
dfs(x,y-1);//递归
}
}
int main()
{
while(1){
cin >> x >> y;
if(x == 0 && y == 0){
break;
return 0;
}
for(int i = 0; i <= y; i++){
for(int j = 0; j <= x; j++){
a[i][j] = '*';//初始化,输入完之后*表示边界(其实是我不喜欢判断的时候加上一行判断语句 )
}
}
for(int i = 1;i <= y; i++){
for(int j =1; j <= x; j++){
cin >> a[i][j];
if(a[i][j] == '@'){
start_y = i;
start_x = j; //记录初始坐标值
// cout << "初始坐标为:" << start_y << ',' << start_x << endl;
}
}
}
dfs(start_x,start_y);
for(int i = 1; i<= y; i++){
for(int j = 1; j <= x; j++){
if(a[i][j] == '$'){
sum++;
}
}
}
cout << sum << endl; //打印计数器
sum = 0; //sum清空
}
return 0;
}
一直都是WA分数为0,不知道错哪里了
我用的可能不算深搜?但我觉得我这个方法有点道理
谢谢!