zoj3890 BFS
2015-08-01 15:13
260 查看
就是搜。
#include<stdio.h> #include<string.h> #include<queue> using namespace std; #define maxn 22 int fang[][2]={{0,1},{1,0},{0,-1},{-1,0}}; int map[maxn][maxn],vis[maxn][maxn][5][5],n; struct node { int count;//计数 int way;//方向 int x; int y; int flag;//是否拿到金子 }; int ok(int x,int y) { if(x<0||x>=n||y<0||y>=n||map[x][y]==1||map[x][y]==2) return 0; return 1; } int bfs() { int i,j; queue<node>q; node temp,t; memset(vis,0,sizeof(vis)); temp.x=temp.y=temp.flag=temp.count=0; temp.way=1; q.push(temp); vis[0][0][1][0]=1; while(!q.empty()) { temp=q.front(); q.pop(); if(temp.count>1000) return -1; if(map[temp.x][temp.y]==3&&!temp.flag) { temp.count++; temp.flag=1; vis[temp.x][temp.y][temp.way][temp.flag]=1; q.push(temp); continue; } if(temp.x==0&&temp.y==0&&temp.flag) { return temp.count+1; } for(i=-1;i<2;i++) { if(i==0) { t.count=temp.count+1; t.flag=temp.flag; t.way=temp.way; t.x=temp.x+fang[t.way][0]; t.y=temp.y+fang[t.way][1]; } else { t.count=temp.count+1; t.flag=temp.flag; t.way=temp.way+i+4; t.way%=4; t.x=temp.x; t.y=temp.y; } if(!vis[t.x][t.y][t.way][t.flag]&&ok(t.x,t.y)) { vis[t.x][t.y][t.way][t.flag]=1; q.push(t); } } } return -1; } int main() { int i,j,t; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(map,0,sizeof(map)); while(1) { int x,y,z; scanf("%d%d%d",&z,&x,&y); if(z==-1&&y==-1&&x==-1) break; map[x][y]=z; } if(map[0][0]==2) { printf("-1\n"); continue; } int ans=bfs(); if(ans==-1) printf("-1\n"); else if(1000-10*ans<0) printf("-1\n"); else printf("%d\n",1000-10*ans); } } /* 1 2 3 1 1 -1 -1 -1 */
相关文章推荐
- 面向对象_多态
- ggplot2--实操(图例的修改:以频数散点图为例)
- Problem D
- 详解Fragment的传值问题
- LeetCode之First Missing Positive
- ZH奶酪:Ubuntu 14.04安装LAMP(Linux,Apache,MySQL,PHP)
- HDU2289——二分——Cup
- html checkbox 自定义样式方法,原有的无法直接通过背景什么改变
- cookie的使用
- 1276 士兵队列训练问题【队列模拟】
- android中跨进程通讯的4种方式
- 算法题:除去字符串里面的括号
- 实现自己的myshell
- 7.数据归档(Archiver)
- unable to execute dex:java heap space 解决方案
- CentOS6 下MySQL option file
- POJ 3469 Dual Core CPU 网络流
- 在网上看的,写的不错
- HDU 2034 今年暑假不AC
- 暴力枚举法求最大连续和