POJ 1924 The Treasure 搜索
来源:互联网 发布:玩转linux系统 编辑:程序博客网 时间:2024/05/21 17:15
题目地址:http://poj.org/problem?id=1924
在N*M的的地图中,有p个怪物,a表示可以攻击周围的怪物,n表示只能攻击当前位置的怪物。
给你每个怪物nge位置,然后每一秒移动到下一个位置,重复移动。
问你最少在100步以内能不能找到宝物。
开始的时候看错了p,第一眼就得是大写,然后一直不能达到,结果复制粘贴才知道是小写,哎……
然后就是在状态里面加上一一维时间。
AC代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <vector>#include <list>#include <deque>#include <queue>#include <iterator>#include <stack>#include <map>#include <set>#include <algorithm>#include <cctype>using namespace std;typedef long long LL;const int N=102;const LL II=1000000007;const int INF=0x3f3f3f3f;const double PI=acos(-1.0);struct point{ int x,y;}pos[N][N],q[2222222],w,e;int n,m,sx,sy,ex,ey,p;char maps[N][N];int t[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};int len[N];int type[N];//怪物的类型,1-可移动,0-不能移动bool vis[N][N][N];//标记数组vis[k][i][j],k-步数,i、j位置bool mon[N][N];//标记当前怪物可以攻击的位置,false不能,true可以void sett(int s,bool k){ int i,j,x,y,tx,ty; for(i=0;i<p;i++) { j=s%len[i];//step后的位置 x=pos[i][j].x; y=pos[i][j].y; mon[x][y]=k;//标记 if(type[i]==0) continue;//如果是0怪物 for(j=0;j<8;j++) { tx=x+t[j][0]; ty=y+t[j][1]; if(tx>=0&&tx<n&&ty>=0&&ty<m) mon[tx][ty]=k; } }}bool pass(int x,int y){ if(maps[x][y]=='#') return false; if(!(x>=0&&x<n&&y>=0&&y<m)) return false; if(mon[x][y]) return false; return true;}int BFS(){ int i,j,k,tx,ty; memset(vis,false,sizeof(vis)); memset(mon,false,sizeof(mon)); int head=0,tail=0,step=0; w.x=sx,w.y=sy; vis[0][sx][sy]=true; q[tail++]=w; while(head<tail&&step<=100) { step++; int size=tail-head; sett(step-1,false);//这个地方的顺序不能反了 sett(step,true); while(size--) { e=q[head]; head++; for(i=0;i<8;i++) { for(j=0;j<=2;j++) { w.x=e.x+j*t[i][0]; w.y=e.y+j*t[i][1]; if(!pass(w.x,w.y)) break;//这个不能写continue,因为不能跳过#或怪物 if(vis[step][w.x][w.y]) continue; vis[step][w.x][w.y]=true; if(w.x==ex&&w.y==ey) return step; q[tail++]=w; } } } } return -1;}int main(){int i,j,T,t,ci=0;while(cin>>n>>m&&(n+m)){ if(ci!=0) puts(""); ci++; p=0; for(i=0;i<n;i++) { scanf("%s",maps[i]); for(j=0;j<m;j++) { if(maps[i][j]=='p') { sx=i,sy=j,maps[i][j]='.'; } else if(maps[i][j]=='t') { ex=i,ey=j,maps[i][j]='.'; } else if(maps[i][j]=='n') { type[p++]=0,maps[i][j]='.'; } else if(maps[i][j]=='a') { type[p++]=1,maps[i][j]='.'; } } } scanf("%d",&p); for(i=0;i<p;i++) { scanf("%d",&len[i]); for(j=0;j<len[i];j++) { scanf("%d%d",&pos[i][j].x,&pos[i][j].y); pos[i][j].x--;//因为从0开始的,所以要减1 pos[i][j].y--; } } int bfs=BFS(); if(bfs==-1) printf("impossible\n"); else printf("%d\n",bfs);}return 0;}
- POJ 1924 The Treasure 搜索
- POJ 1924 The Treasure
- poj 1924 The Treasure (bfs+地图预处理)
- 【POJ】1164【搜索】【The Castle】
- poj 2594 Treasure Exploration
- POJ 2594 Treasure Exploration
- poj 2594 Treasure Exploration
- poj 2594 Treasure Exploration
- poj 1066 Treasure Hunt
- POJ 1066 Treasure Hunt
- POJ 1066 Treasure Hunt
- POJ 1066 Treasure Hunt
- poj 2594 Treasure Exploration
- POJ 2594 Treasure Exploration
- POJ 2594 Treasure Exploration
- poj 1066 Treasure Hunt
- POJ 2594 Treasure Exploration
- POJ 1066 Treasure Hunt
- Boost库之program_options的使用
- Tree树状结构显示(dtree、xtree、ExtTree、E3tree、ztree)
- 【diannaoxitong】系统做好提前预防让病毒无从下手
- 过滤器与拦截器的区别
- 项目经理需要修炼的9件事
- POJ 1924 The Treasure 搜索
- AlertDialog对话框的建立方法
- Oracle 权限管理
- 学生站队问题
- REST Service 基础
- php判断用户是否来自手机客户端
- 暑假第一场D.Brackets
- android 复制文件到另一个文件夹里面
- 默认构造函数