解题思路:
注意事项:
参考代码:
#include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<bits/stdc++.h> using namespace std; int n,m,k,sx,sy; int a[1001][1001]; int fy[6]={0,0,1,0,-1}; int fx[6]={0,1,0,-1,0}; struct Node { int x; int y; int zt; }; char ss; int endstep; queue<Node> q; int tx,ty; void bfs() { Node startnode; startnode.x=sx; startnode.y=sy; startnode.steps=1; q.push(startnode); while(true) { Node newnode; if(q.front().steps-1==endstep){ return;} if(a[q.front().x][q.front().y]==0) { if(k==4) { k=1;newnode.x=q.front().x+fx[k]; newnode.y=q.front().y+fy[k]; } else{ //cout<<'Y'; k++;newnode.x=q.front().x+fx[k]; newnode.y=q.front().y+fy[k]; } newnode.steps=q.front().steps+1; a[q.front().x][q.front().y]=1; // cout<<"白格"<<newnode.x<<' '<<newnode.y<<endl; } else if(a[q.front().x][q.front().y]==1) { if(k==1) { k=4;newnode.x=q.front().x+fx[k]; newnode.y=q.front().y+fy[k]; } else { k--;newnode.x=q.front().x+fx[k]; newnode.y=q.front().y+fy[k]; } newnode.steps=q.front().steps+1; a[q.front().x][q.front().y]=0; // cout<<"黑格"<<newnode.x<<' '<<newnode.y<<endl; } // cout<<newnode.x<<' '<<newnode.y<<endl; q.push(newnode); q.pop(); } } int main() { cin>>n>>m; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>a[i][j]; } } cin>>sx>>sy>>ss>>endstep; if(ss=='L') k=4; else if(ss=='R') k=2; else if(ss=='U') k=3; else if(ss=='D') k=1; bfs(); cout<<q.back().x<<' '<<q.back().y<<endl; return 0; }
0.0分
1 人评分
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:540 |
WU-小九九 (C++代码)浏览:1684 |
简单的a+b (C语言代码)浏览:596 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:1419 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:1068 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:566 |
川哥的吩咐 (C语言代码)浏览:610 |
交换Easy (C语言代码)浏览:761 |
简单的a+b (C语言代码)浏览:449 |
【偶数求和】 (C语言代码)浏览:433 |