搜索的入门题
#include "stdio.h"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
char fi[105][105];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int move[8][2]= {0,1,1,1,1,0,1,-1,0,-1,-1,-1,-1,0,-1,1};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int qu[10005][2];
int head,tail;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void begin()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
head=0;
tail=-1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void inq(int l,int c)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
++tail;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
qu[tail][0]=l;
qu[tail][1]=c;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void outq()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
head++;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int empty()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
if(head>tail)return 1;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void search(int l,int c,int n,int m)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int tl,tc,i;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
begin();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
inq(l,c);
fi[l][c]='.';
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(!empty())
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
tl=qu[head][0];
tc=qu[head][1];
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
outq();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for(i=0;i<8;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
tl+=move[i][0];
tc+=move[i][1];
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(tl>=0&&tl<n&&tc>=0&&tc<m)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(fi[tl][tc]=='W')
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
inq(tl,tc);
fi[tl][tc]='.';
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
tl-=move[i][0];
tc-=move[i][1];
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int n,m;
int i,j;
int count;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(scanf("%d%d",&n,&m)!=EOF)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for(i=0;i<n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf("%s",&fi[i][0]);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
count=0;
for(i=0;i<n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for(j=0;j<m;j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(fi[i][j]=='W')
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
search(i,j,n,m);
count++;
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
printf("%d\n",count);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
目,BFS和DFS都可以的,地址: http://acm.pku.edu.cn/JudgeOnline/problem?id=2386
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
28 | 29 | 30 | 31 | 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 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
公告
决定从线程开始!!
常用链接
留言簿(6)
随笔档案
搜索
最新评论
![](/images/xml.gif)
阅读排行榜
评论排行榜
|
|