TYVJ 1261 可达总数 (BFS)

可达总数

From  Moe-ing
 
 
 
 
 
 
背景 Background
蛟川书院模拟试题
 
 
 
 
描述 Description
话说小明在你的帮助下,破密了Ferrari设的第二密码门,正要往前走,不料,前面有面墙,奇怪的事情发生了……

他收到一封来自Ferrari的信,上面写着“哈,你很聪明,但是你已经进入了我的圈套中,实话告诉你,第二关每个结果都应该减一,这里是验证区,看你能不能闯过去了!”小明知道前面的墙有a行b列,Ferrari已在第x行、第y列埋上了炸弹,如果从炸弹开始走(指向上、向下、向左、向右)n步之内(包括0步)的墙就被入侵了,他要把所有入侵的点全部找出来,否则他就有生命危险。请你告诉他有多少墙被入侵了。
 
 
 
 
输入格式 InputFormat
输入文件kdzs.in共1行,为a,b,x,y,n。
 
 
 
 
输出格式 OutputFormat
输出文件kdzs.out共1行,就是多少墙被入侵了。
 
 
 
 
样例输入 SampleInput 
6 7 3 2 4
 
 
 
 
样例输出 SampleOutput 
27
 
 
 
 
数据范围和注释 Hint
10%的数据满足: 1<=x<=a<=100 1<=y<=b<=100 1<=n<=10
100%的数据满足: 1<=x<=a<=1000 1<=y<=b<=1000 1<=n<=100

 
 
 
 
来源 Source
From hlx1996
 
BFS记录下步数,统计步数小于n的个数
 1 #include<cstdio>

 2 #include<cmath>

 3 #include<queue>

 4 #include<cstring>

 5 #include<stdlib.h>

 6 #include<algorithm>

 7 using namespace std;

 8 const int MAXN=1000+10;

 9 const int INF=-0x3f3f3f3f;

10 struct node

11 {

12     int x,y;

13     int step;

14 }p;

15 int starx,stary,ans;

16 int n,m,x,y,ok;

17 int vis[MAXN][MAXN];

18 int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

19 void BFS()

20 {

21     memset(vis,0,sizeof(vis));

22     queue<node> Q;

23     ans=0;

24     p.x=starx;

25     p.y=stary;

26     p.step=0;

27     Q.push(p);

28     while(!Q.empty())

29     {

30         node now,next;

31         now=Q.front();

32         Q.pop();

33         for(int i=0;i<4;i++)

34         {

35             next.x=now.x+dir[i][0];

36             next.y=now.y+dir[i][1];

37             next.step=now.step+1;

38             if(0<=next.x && next.x<n && 0<=next.y && next.y<m && !vis[next.x][next.y] && next.step<=ok)

39             {

40                 vis[next.x][next.y]=1;

41                 ans++;

42                 Q.push(next);

43             }

44         }

45     }

46 }

47 int main()

48 {

49     //freopen("in.txt","r",stdin);

50     scanf("%d %d %d %d %d",&n,&m,&x,&y,&ok);

51     starx=x-1;

52     stary=y-1;

53     BFS();

54     printf("%d\n",ans);

55     return 0;

56 }
View Code

 

你可能感兴趣的:(bfs)