AOJ-579-期末考試之考試傳紙條

來源:互聯網
上載者:User

AOJ-579-期末考試之考試傳紙

http://icpc.ahu.edu.cn/OJ/Problem.aspx?id=579

BFS,按模版寫的,比賽時資料輸入處理弄錯了,哎。。。太弱了。。。

#include<stdio.h>#include<string.h>#include<stdlib.h>int n,m;char map[105][105];int ans[105][105];int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};typedef struct{int x;int y;}node;int bfs(node s,node e){node que[10005];node tt;int head=0,tail=1,i;que[0]=s;ans[s.x][s.y]=0;map[s.x][s.y]='#';while(head<tail){s=que[head++];for(i=0;i<4;i++){tt.x=s.x+dir[i][0];tt.y=s.y+dir[i][1];if(tt.x>=0&&tt.x<n&&tt.y>=0&&tt.y<m&&map[tt.x][tt.y]!='#'){ans[tt.x][tt.y]=ans[s.x][s.y]+1;if(tt.x==e.x&&tt.y==e.y)return ans[e.x][e.y];map[tt.x][tt.y]='#';que[tail++]=tt;}}}return 0;}int main(){int i,j;char ch;node s,e;int k1,k2;int ans;while(scanf("%d %d",&n,&m)!=EOF){memset(map,0,sizeof(map));for(i=0;i<n;i++){getchar();for(j=0;j<m;j++){scanf("%c",&ch);if(ch=='A'){s.x=i;s.y=j;}else if(ch=='B'){e.x=i;e.y=j;}    else if(ch=='T')  //監考老師的一圈都是不可達的{for(k1=i-1;k1<=i+1;k1++)for(k2=j-1;k2<=j+1;k2++)if(k1>=0&&k1<n&&k2>=0&&k2<m)map[k1][k2]='#';}if(map[i][j]!='#')map[i][j]=ch;}}if(map[s.x][s.y]=='#'||map[e.x][e.y]=='#'){printf("-1\n");continue;}ans=bfs(s,e);if(ans)printf("%d\n",ans);elseprintf("-1\n");}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.