SSL P2278 Oliver的救援

来源:互联网 发布:c 定义结构体数组 编辑:程序博客网 时间:2024/05/16 11:27

题目:http://blog.csdn.net/qq_35786326/article/details/78836981

题意:

  一步一步的走,求最短路径数

分析:

  这是一个启蒙的广搜难(shui)题。

代码:

#include<iostream>#include<cstdio>#include<algorithm>#include<string>#define LL long longusing namespace std;inline LL read(){LL d=0,f=1;char s=getchar();while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}return d*f;}int a,b,c,d,n,m,x[1010][1010],state[1000001][3],dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};void bfs(){int head,tail,x1,y1;head=0;tail=1;state[1][0]=a;state[1][1]=b;state[1][2]=0;x[1][1]=1;    do    {    head++;for(int i=0;i<4;i++){x1=state[head][0]+dx[i];y1=state[head][1]+dy[i];if(x[x1][y1]<=0&&x1>0&&x1<=n&&y1>0&&y1<=n)//执行条件{x[x1][y1]=1;tail++;state[tail][0]=x1;state[tail][1]=y1;state[tail][2]=state[head][2]+1;if(x1==c&&y1==d) {printf("%d",state[tail][2]);tail=0;return;}//终止操作}}    }while(head<tail);return;}int main(){n=read();int i,j;char s;for(i=1;i<=n;i++){j=0;while(s=getchar(),s!='\n')//字符输入{j++;if (s=='0') x[i][j]=0;else x[i][j]=1;}}a=read();b=read();c=read();d=read();bfs();return 0;}


原创粉丝点击