【搜索】[luoguP1126]机器人搬重物
2017-10-31 07:34
190 查看
题目
广搜题 很生 。
qx表示横坐标 qy表示纵坐标 qd表示方向 其他的看代码吧
代码如下
广搜题 很生 。
qx表示横坐标 qy表示纵坐标 qd表示方向 其他的看代码吧
代码如下
#include<iostream> #include<cstdio> #include<cctype> #include<queue> #include<cstdlib> using namespace std; #define in = read(); typedef long long ll; typedef unsigned int ui; const ll size = 50 + 5; queue<int>qx , qy , qd; int n , m; int sx , sy , tx , ty , num , x , y , d , x1 , y1 , d1; int a[size][size][size]; bool map[size][size]; int heng[4] = {0 , 1 , 0 , -1} , shu[4] = {1 , 0 , -1 , 0}; char c; inline ll read(){ ll num = 0 , f = 1; char ch = getchar(); while(!isdigit(ch)){ if(ch == '-') f = -1; ch = getchar(); } while(isdigit(ch)){ num = num*10 + ch - '0'; ch = getchar(); } return num*f; } inline void bfs(){ while(!qx.empty()){ x = qx.front(); y = qy.front(); d = qd.front(); qx.pop(); qy.pop(); qd.pop(); for(register int i=1;i<=3;i++){ x1 = x + heng[d]*i; y1 = y + shu[d]*i; if(x1 <= 0 || x1 >= n || y1 <= 0 || y1 >= m || map[x1][y1]) break; if(x1 == tx && y1 == ty){ printf("%d" , a[x][y][d]); exit(0); } if(!a[x1][y1][d]){ a[x1][y1][d] = a[x][y][d] + 1; qx.push(x1); qy.push(y1); qd.push(d); } } d1 = d + 1; if(d1 == 4) d1 = 0; if(!a[x][y][d1]){ a[x][y][d1] = a[x][y][d] + 1; qx.push(x); qy.push(y); qd.push(d1); } if(!d) d1 = 3; else d1 = d - 1; if(!a[x][y][d1]){ a[x][y][d1] = a[x][y][d] + 1; qx.push(x); qy.push(y); qd.push(d1); } } } int main(){ n in; m in; for(register int i=1;i<=n;i++) for(register int j=1;j<=m;j++){ num in; if(num) map[i][j] = map[i - 1][j - 1] = map[i - 1][j] = map[i][j - 1] = 1; } sx in; sy in; tx in; ty in; cin>>c; if(sx == tx && sy == ty){ printf("0"); return 0; } if(map[tx][ty]){ printf("-1"); return 0; } qx.push(sx); qy.push(sy); if(c == 'E') a[sx][sy][0]=1 , qd.push(0); else if(c == 'S') a[sx][sy][1]=1 , qd.push(1); else if(c == 'W') a[sx][sy][2]=1 , qd.push(2); else if(c == 'N') a[sx][sy][3]=1 , qd.push(3); bfs(); printf("-1"); return 0; } //COYG
相关文章推荐
- 洛谷网 1126机器人搬重物 优先队列搜索 一般
- 洛谷 1126 机器人搬重物
- 普及练习场 广度优先搜索 机器人搬重物
- 【BFS】洛谷1126机器人搬重物_water(划掉)
- [luoguP1013] 进制位(搜索)
- hiho 1170(机器人-记忆化搜索+状态压缩)
- 下一个软件开发的浪潮将从搜索机器人和人工智能兴起
- 洛谷 P1126 机器人搬重物
- 51nod 1307 绳子与重物【并查集】【搜索】【二分法】
- 南邮 OJ 1583 机器人搬重物
- Perl 关键词搜索机器人
- 巧用web机器人搜索廉价二手房
- 洛谷P1126 机器人搬重物
- luogu P1126 机器人搬重物
- 洛谷—— P1126 机器人搬重物
- 搜索 HOJ 1126 Smith Numbers
- 巧用web机器人搜索廉价二手房-PYTHON
- P1126 机器人搬重物
- 【The first】 题解:机器人搬重物
- LuoguP1441 砝码称重 解题报告【搜索+背包型DP】