校第十六届大学生程序设计竞赛暨2016省赛集训队选拔赛1002
2016-03-20 21:17
337 查看
Problem B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 254 Accepted Submission(s): 11
Problem Description
As you know, mazes in some role-playing games are quite complicated. Usually, it should take a long time to keep trying different ways to get out of a maze. But here comes a question, that with the help of algorithm and computer, can we figure out a faster
way to solve a maze?
So we are taking a experiment here. Now we have a maze in a shape of an N times M lattice (N rows and M columns). In the lattice, some grids are empty so that we may stand on them, while others are obstacles and it's forbidden to locate on these grids. In each
move, we can make a move to the 8 adjacent empty grids with an exception: we cannot move through two obstacles' gap nor outside the maze. The following figure shows the moving rules.
Figure 1: Moving rules, in which white for empty grids and grey for obstacles
In order to get out of it, we need to trigger activations in the maze. Totally there are K activations,
each located on a specific empty grid. If we are standing on a grid with an activation, it will be triggered (and it disappears immediately). In the maze, our target is to trigger all K activations
in order. When the last activation is triggered, we may leave it at once.
Now we have already known the positions of obstacles and activations. Initially, we are standing on a specific grid in the maze. Can you figure out how many moves we should make at least to get out of it?
Input
The number of test cases T(T≤20) will
occur in the first line of input.
For each test case:
The first line contains the number of rows N(2≤N≤100),
columns M(2≤M≤100) and
activations K(1≤K≤10).
Each of the following N lines
contains M characters,
where a '#' denotes an obstacle and a '.' denotes an empty grid.
The following line contains the start position (x,y),
which denotes the grid on the xth
row and the yth
column. It's always an empty grid.
Each of the following K lines
describe a coordinate of the activations. All activations will not occur on obstacles, and two activations will not share the same grid. We need to trigger all of them in order of the given list.
Output
For each test case, output the minimum moves we need to make to reach the target. If we cannot get out of the maze, output -1 instead.
Sample Input
3 3 3 2 ... ... ... 1 1 1 3 2 2 3 3 1 ... .#. ... 1 1 3 3 2 3 1 ..# .#. 1 1 2 3
Sample Output
3 3 -1WA了一万年,这题卡住之后一直没心思做接下来的题目了 被卡地方 1.必须要按顺序经过所有点,也就是说不能提前经过有标记的点,必须按顺序 2.要经过的点可能为起点吃一堑长一智,以后要看清楚题目#include<cstdio> #include<cstring> #include<queue> using namespace std; struct node { int x,y,d; }s[1005],u; int dir[9][2],T,i,n,m,k,flag,flag1,ans,j; char g[105][105]; int vis[105][105]; int ok(node t,int ty) { t.x--;t.y--; if(t.x<0||t.x>=n||t.y<0||t.y>=m) return 0; if(g[t.x][t.y]=='#') return 0; if(ty==1&&g[t.x+1][t.y]=='#'&&g[t.x][t.y+1]=='#') return 0; if(ty==3&&g[t.x][t.y-1]=='#'&&g[t.x+1][t.y]=='#') return 0; if(ty==6&&g[t.x-1][t.y]=='#'&&g[t.x][t.y+1]=='#') return 0; if(ty==8&&g[t.x-1][t.y]=='#'&&g[t.x][t.y-1]=='#') return 0; return 1; } int main() { dir[1][0]=-1; dir[1][1]=-1; dir[2][0]=-1; dir[2][1]=0; dir[3][0]=-1; dir[3][1]=1; dir[4][0]=0; dir[4][1]=-1; dir[5][0]=0; dir[5][1]=1; dir[6][0]=1; dir[6][1]=-1; dir[7][0]=1; dir[7][1]=0; dir[8][0]=1; dir[8][1]=1;node t; scanf("%d",&T); while(T--) { queue<node>q; while(!q.empty()) q.pop(); scanf("%d%d%d",&n,&m,&k); for(i=0;i<n;i++) scanf("%s",g[i]); memset(vis,0,sizeof(vis)); scanf("%d%d",&s[0].x,&s[0].y); for(i=1;i<=k;i++) { scanf("%d%d",&s[i].x,&s[i].y); } flag=1; ans=0; for(j=0;j<k;j++) { memset(vis,0,sizeof(vis)); vis[s[j].x][s[j].y]=1; for(i=j+2;i<=k;i++) vis[s[i].x][s[i].y]=1; if(flag) { if(s[0].x==s[j+1].x&&s[0].y==s[j+1].y) { if(j!=0){flag=0;break;} else continue; } u.x=s[j].x;u.y=s[j].y;u.d=0; q.push(u); flag1=1; while(!q.empty()) { u=q.front(); q.pop(); for(i=1;i<=8;i++) { t.x=u.x+dir[i][0]; t.y=u.y+dir[i][1]; t.d=u.d+1; if(ok(t,i)&&!vis[t.x][t.y]) { q.push(t); vis[t.x][t.y]=1; if(t.x==s[j+1].x&&t.y==s[j+1].y) { flag1=0; ans+=t.d; while(!q.empty()) q.pop(); break; } } } } if(flag1) flag=0; } } if(flag) printf("%d\n",ans); else printf("-1\n"); } return 0; }
相关文章推荐
- Python笔记(4)——Python Web框架
- python 面向对象编程学习
- vs2013+opencv2.4.11+Qt5.5.1配置
- 关于地图初始化的一些尝试与疑问
- 猎豹MFC--MiNiMS管理系统2增加修改数据库
- 程序员对产品的思考暨项目总结:一品茶香
- 寻找第k小的数字
- No.4小白的HTML+CSS心得篇
- 2.0-创建快照
- css实现垂直水平居中
- HttpHandler与HttpModule的用处与区别
- HTTP 错误 404.17 - Not Found
- Spark生态系统项目
- Silicon Lab Ember zigbee学习杂谈---zcl frame解析
- poj2823线段树
- 47. Permutations II | Java最短代码实现
- 汇编学习1
- 类指针
- spring学习笔记(8)AOP增强(advice)配置与应用
- [c++]链表实现--持续更。