|
<
P1126 机械人搬重物
传收门
那讲题本来出啥好道的,但细节其实比力多,被坑了很多多少次。
- 起首输进的是格子图,需求转化成面图,具体操作是
- 最坑的一个面正在于,平常写宽搜的时分,碰到出鸿沟大概不克不及会见的面时,皆是间接进进下一层轮回(continue),但正在那讲题中,因为能够走1~3步,那末当途径上呈现停滞时,则不克不及停止下一轮轮回,需求break。
代码:
- #include <bits/stdc++.h>
- #define MAX 55
- using namespace std;
- int mod(int x){
- return (x+4)%4;
- }
- struct pt{
- int x, y, dir, step;
- pt(){}
- pt(int a, int b, int c, int d):x(a), y(b), dir(c), step(d){}
- };
- const int movx[] = {0,1,0,-1}, movy[] = {1,0,-1,0};
- int a[MAX][MAX];
- bool vis[MAX][MAX][5];
- int n, m;
- pt st, ed;
- void bfs(){
- queue<pt> q;
- bool flag = false;
- st.step = 0;
- q.push(st);
- vis[st.x][st.y][st.dir] = true;
- while(!q.empty()){
- pt t = q.front();
- q.pop();
- if(t.x == ed.x && t.y == ed.y){
- cout << t.step << endl;
- flag = true;
- break;
- }
- for(int i = 1; i <= 3; i++){
- int u, v;
- u = t.x + i*movx[t.dir];
- v = t.y + i*movy[t.dir];
- if(u<=0 || u>=n || v<=0 || v>=m || a[u][v] == 1){
- break;
- }
- if(vis[u][v][t.dir]){
- continue;
- }
- vis[u][v][t.dir] = true;
- q.push(pt(u, v, t.dir, t.step+1));
- }
- if(!vis[t.x][t.y][mod(t.dir+1)]){
- vis[t.x][t.y][mod(t.dir+1)] = true;
- q.push(pt(t.x, t.y, mod(t.dir+1), t.step+1));
- }
- if(!vis[t.x][t.y][mod(t.dir-1)]){
- vis[t.x][t.y][mod(t.dir-1)] = true;
- q.push(pt(t.x, t.y, mod(t.dir-1), t.step+1));
- }
- }
- if(!flag){
- cout << -1 << endl;
- }
- }
- int main()
- {
- cin >> n >> m;
- for(int i = 1; i <= n; i++){
- for(int j = 1; j <= m; j++){
- scanf("%d", &a[i][j]);
- if(a[i][j] == 1){
- a[i-1][j-1] = a[i-1][j] = a[i][j-1] = 1;
- }
- }
- }
- cin >> st.x >> st.y >> ed.x >> ed.y;
- char c;
- cin >> c;
- switch(c){
- case 'E':
- st.dir = 0; break;
- case 'S':
- st.dir = 1; break;
- case 'W':
- st.dir = 2; break;
- case 'N':
- st.dir = 3; break;
- }
- bfs();
-
- return 0;
- }
复造代码
免责声明:假如进犯了您的权益,请联络站少,我们会实时删除侵权内乱容,感谢协作! |
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
|