Oil Deposits 搜索入门水题。。。好久没打C++ 结果还卡了。。。惭愧惭愧
先给出AC代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
using namespace std;
typedef struct node
{
int x;
int y;
}node;
void BFS(int i,int j,char map[][100+10],int visited[][100+10],int m,int n)
{
node xx;
xx.x = i;
xx.y = j;
queue<node>q;
q.push(xx);
visited[i][j] = 1;
while(!q.empty())
{
node now;
now = q.front();
if( (map[now.x-1][now.y]=='@') && (!visited[now.x-1][now.y]) && (now.x-1>=0) )
{
node last;
last.x = now.x-1;
last.y = now.y;
q.push(last);
visited[last.x][last.y]=1;
}
if( (map[now.x+1][now.y]=='@' && !visited[now.x+1][now.y]) && (now.x+1 <m))
{
node last;
last.x = now.x+1;
last.y = now.y;
q.push(last);
visited[last.x][last.y]=1;
}
if( (map[now.x][now.y-1]=='@' && !visited[now.x][now.y-1]) && (now.y-1>=0 ))
{
node last;
last.x = now.x;
last.y = now.y-1;
q.push(last);
visited[last.x][last.y]=1;
}
if( (map[now.x][now.y+1]=='@' && !visited[now.x][now.y+1]) && (now.y+1 <n) )
{
node last;
last.x = now.x;
last.y = now.y+1;
q.push(last);
visited[last.x][last.y]=1;
}
if( (map[now.x+1][now.y+1]=='@' && !visited[now.x+1][now.y+1]) && (now.x+1 <m) && (now.y+1<n) )
{
node last;
last.x = now.x+1;
last.y = now.y+1;
q.push(last);
visited[last.x][last.y]=1;
}
if( (map[now.x-1][now.y+1]=='@' && !visited[now.x-1][now.y+1]) && (now.x-1>=0) && (now.y+1<n))
{
node last;
last.x = now.x-1;
last.y = now.y+1;
q.push(last);
visited[last.x][last.y]=1;
}
if( (map[now.x+1][now.y-1]=='@' && !visited[now.x+1][now.y-1]) && (now.x+1<m) && (now.y-1 >=0) )
{
node last;
last.x = now.x+1;
last.y = now.y-1;
q.push(last);
visited[last.x][last.y]=1;
}
if( (map[now.x-1][now.y-1]=='@' && !visited[now.x-1][now.y-1]) && (now.x-1>=0) && (now.y-1>=0))
{
node last;
last.x = now.x-1;
last.y = now.y-1;
q.push(last);
visited[last.x][last.y]=1;
}
q.pop();
}
}
int main()
{
int m,n;
while(~scanf("%d%d",&m,&n) && m!=0 )
{
// if(m==0 || n==0)
// break;
getchar();
char map[100+10][100+10];
int visited[100+10][100+10]={0};
for(int i=0;i<m;i++)
scanf("%s",map[i]);
int cnt = 0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(map[i][j]=='@' && !visited[i][j])
{
BFS(i,j,map,visited,m,n);
cnt++;
}
}
cout<<cnt<<"\n";
}
return 0;
}
当时卡的是这里。。。 <=m 多了个等于号。。数组越界,存取不确定值。。。天呐。。。
因为当时代码在 POJ 上过了。。。HDU 上跪了。。。找了好久