题目很好懂,只搜索能到达的地方,统计数量,啥也不做。很坑爹的地方是,行列输入是反的!
AC代码:31MS 364K,空间还可以优化,直接不用开visit数组,直接把访问后的点置为障碍即可。
[cpp]
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct node1{
int x,y;
}start,p,q;
queue
bool visit[20][20];
char maze[20][20];
int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};//shang,右,xia,左
int m,n,cnt;
bool isbond(node1 &a){
if(a.x<0 || a.x>=m || a.y<0 || a.y>=n || maze[a.x][a.y]=='#')return 1;
return 0;
}
int bfs()
{
while(!Q.empty())Q.pop();
cnt=1;
memset(visit,0,sizeof(visit));
Q.push(start);
visit[start.x][start.y]=1;
while(!Q.empty())
{
p=Q.front();
Q.pop();
for(int i=0;i<4;i++)
{
q.x=p.x+dir[i][0];
q.y=p.y+dir[i][1];
if(isbond(q))continue;
if(visit[q.x][q.y])continue;
cnt++;
visit[q.x][q.y]=1;
Q.push(q);
}
}
return cnt;
}
int main()
{
//ifstream fin;
//fin.open("data1.txt");
while(cin>>n>>m)
{
if(m==0&&n==0)break;
for(int i=0;i
if(maze[i][j]=='@'){
start.x=i;
start.y=j;
}
}
}
cout<
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct node1{
int x,y;
}start,p,q;
queue
bool visit[20][20];
char maze[20][20];
int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};//shang,右,xia,左
int m,n,cnt;
bool isbond(node1 &a){
if(a.x<0 || a.x>=m || a.y<0 || a.y>=n || maze[a.x][a.y]=='#')return 1;
return 0;
}
int bfs()
{
while(!Q.empty())Q.pop();
cnt=1;
memset(visit,0,sizeof(visit));
Q.push(start);
visit[start.x][start.y]=1;
while(!Q.empty())
{
p=Q.front();
Q.pop();
for(int i=0;i<4;i++)
{
q.x=p.x+dir[i][0];
q.y=p.y+dir[i][1];
if(isbond(q))continue;
if(visit[q.x][q.y])continue;
cnt++;
visit[q.x][q.y]=1;
Q.push(q);
}
}
return cnt;
}
int main()
{
//ifstream fin;
//fin.open("data1.txt");
while(cin>>n>>m)
{
if(m==0&&n==0)break;
for(int i=0;i
if(maze[i][j]=='@'){
start.x=i;
start.y=j;
}
}
}
cout<
return 0;
}