Codeforces Round #354 (Div. 2) D. Theseus and labyrinth(BFS)
2016-06-03 00:35
477 查看
传送门:点击打开链接
D. Theseus and labyrinth
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of size n × m and
consists of blocks of size 1 × 1.
Each block of the labyrinth has a button that rotates all blocks 90 degrees
clockwise. Each block rotates around its center and doesn't change its position in the labyrinth. Also, each block has some number of doors (possibly none). In one minute, Theseus can either push the button in order to rotate all the blocks 90 degrees
clockwise or pass to the neighbouring block. Theseus can go from block A to some neighbouring block B only
if block A has a door that leads to block B and
block B has a door that leads to block A.
Theseus found an entrance to labyrinth and is now located in block (xT, yT) —
the block in the row xT and
column yT.
Theseus know that the Minotaur is hiding in block (xM, yM) and
wants to know the minimum number of minutes required to get there.
Theseus is a hero, not a programmer, so he asks you to help him.
Input
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 1000) —
the number of rows and the number of columns in labyrinth, respectively.
Each of the following n lines contains m characters,
describing the blocks of the labyrinth. The possible characters are:
«+» means this block has 4 doors (one door
to each neighbouring block);
«-» means this block has 2 doors — to the
left and to the right neighbours;
«|» means this block has 2 doors — to the
top and to the bottom neighbours;
«^» means this block has 1 door — to the
top neighbour;
«>» means this block has 1 door — to the
right neighbour;
«<» means this block has 1 door — to the
left neighbour;
«v» means this block has 1 door — to the
bottom neighbour;
«L» means this block has 3 doors — to all
neighbours except left one;
«R» means this block has 3 doors — to all
neighbours except right one;
«U» means this block has 3 doors — to all
neighbours except top one;
«D» means this block has 3 doors — to all
neighbours except bottom one;
«*» means this block is a wall and has no doors.
Left, right, top and bottom are defined from representing labyrinth as a table,
where rows are numbered from 1 to n from
top to bottom and columns are numbered from 1 to m from
left to right.
Next line contains two integers — coordinates of the block (xT, yT) (1 ≤ xT ≤ n, 1 ≤ yT ≤ m),
where Theseus is initially located.
Last line contains two integers — coordinates of the block (xM, yM) (1 ≤ xM ≤ n, 1 ≤ yM ≤ m),
where Minotaur hides.
It's guaranteed that both the block where Theseus starts and the block where Minotaur is hiding have at least one door. Theseus and Minotaur may be initially located at the same block.
Output
If Theseus is not able to get to Minotaur, then print -1 in the only line of the output. Otherwise, print the minimum number of minutes required to get to the
block where Minotaur is hiding.
Examples
input
output
input
output
Note
Assume that Theseus starts at the block (xT, yT) at
the moment 0.
题意:一张图,一堆点,几扇门,能否到终点,不能则输出-1。
思路:这题体现了审清题目是十分重要的...红字比较关键,一般(wo)的思维是1为底 n为顶,白白贡献了N次WA,还有坐标的XY轴绝对不能弄反了。不然就会出现这种莫名其妙的WA:
2(在机器上提交和在codeforces提交的结果不同,说到底还是程序写的不对,但不容易查错)
Time: 0 ms, memory: 21804 KB
Verdict: WRONG_ANSWER
Input
Participant's output
Jury's answer
Checker comment
剩下的就比较简单了,一次90°,可以转换为四张地图,也就是三维bfs。
其他要注意的点:1.转一次是全图都转,而不是仅仅所在的那块。
2.转动为顺时针90°,而并非逆时针。
3.起点和终点可能为同一个点。
4.俩块间必须相互连通才能移动。
#include<bits/stdc++.h>
using namespace std;
int n,m;
char mp[4][1005][1005]={0};
int hav[4][1005][1005]={0};
struct node{
int a;
int b;
int time;
int type;
friend bool operator < (node a,node b){
return a.time>b.time;
}
};
int bg1,bg2,ed1,ed2;
int dir[5][2]={{0,1},{1,0},{0,-1},{-1,0},{0,0}};
bool check(int x1,int y1,int di,int type){
if(di==4) return true;
if(di==1){
char c=mp[type][x1][y1];
char d=mp[type][x1+dir[di][0]][y1+dir[di][1]];
if(c=='+'||c=='|'||c=='v'||c=='L'||c=='R'||c=='U'){
if(d=='+'||d=='|'||d=='^'||d=='L'||d=='R'||d=='D'){
return true;
}
}
return false;
}
else if(di==0){
char c=mp[type][x1][y1];
char d=mp[type][x1+dir[di][0]][y1+dir[di][1]];
if(c=='+'||c=='-'||c=='>'||c=='L'||c=='D'||c=='U'){
if(d=='+'||d=='-'||d=='<'||d=='U'||d=='R'||d=='D'){
return true;
}
}
return false;
}
else if(di==3){
char c=mp[type][x1][y1];
char d=mp[type][x1+dir[di][0]][y1+dir[di][1]];
if(d=='+'||d=='|'||d=='v'||d=='L'||d=='R'||d=='U'){
if(c=='+'||c=='|'||c=='^'||c=='L'||c=='R'||c=='D'){
return true;
}
}
return false;
}
else if(di==2){
char c=mp[type][x1][y1];
char d=mp[type][x1+dir[di][0]][y1+dir[di][1]];
if(d=='+'||d=='-'||d=='>'||d=='L'||d=='D'||d=='U'){
if(c=='+'||c=='-'||c=='<'||c=='U'||c=='R'||c=='D'){
return true;
}
}
return false;
}
}
int bfs(int x,int y,int type){
if(bg1==ed1&&bg2==ed2) return 0;
node first,next;
first.a=x,first.b=y,first.time=0;
first.type=type;
priority_queue<node>que;
que.push(first);
int fx,fy,fz;
while(!que.empty()){
first=que.top();
que.pop();
for(int i=0;i<5;i++){
if(i!=4){
fx=dir[i][0]+first.a;
fy=dir[i][1]+first.b;
fz=first.type;
}
else{
fx=first.a;
fy=first.b;
fz=(first.type+1)%4;
}
if(fx>=0&&fx<n&&fy>=0&&fy<m&&check(first.a,first.b,i,fz)&&!hav[fz][fx][fy]){
if(fx==ed1-1&&fy==ed2-1){
return first.time+1;
}
else nex
a889
t.time=first.time+1;
next.a=fx;
next.b=fy;
next.type=fz;
hav[fz][fx][fy]=1;
que.push(next);
}
}
}
return -1;
}
int main(void){
#define _Araragi_Tsukihi
#ifndef _Araragi_Tsukihi
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",mp[0][i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
switch(mp[0][i][j]){
case '+':
for(int k=1;k<4;k++) mp[k][i][j]='+';
break;
case '-':
mp[1][i][j]=mp[3][i][j]='|';
mp[2][i][j]='-'; break;
case '|':
mp[1][i][j]=mp[3][i][j]='-';
mp[2][i][j]='|'; break;
case '^':
mp[1][i][j]='>'; mp[2][i][j]='v'; mp[3][i][j]='<';
break;
case '>':
mp[1][i][j]='v'; mp[2][i][j]='<'; mp[3][i][j]='^';
break;
case '<':
mp[1][i][j]='^'; mp[2][i][j]='>'; mp[3][i][j]='v';
break;
case 'v':
mp[1][i][j]='<'; mp[2][i][j]='^'; mp[3][i][j]='>';
break;
case 'L':
mp[1][i][j]='U'; mp[2][i][j]='R'; mp[3][i][j]='D';
break;
case 'R':
mp[1][i][j]='D'; mp[2][i][j]='L'; mp[3][i][j]='U';
break;
case 'U':
mp[1][i][j]='R'; mp[2][i][j]='D'; mp[3][i][j]='L';
break;
case 'D':
mp[1][i][j]='L'; mp[2][i][j]='U'; mp[3][i][j]='R';
break;
case '*':
for(int k=1;k<4;k++) mp[k][i][j]='*';
break;
}
}
scanf("%d%d%d%d",&bg1,&bg2,&ed1,&ed2);
printf("%d\n",bfs(bg1-1,bg2-1,0));
return 0;
}
D. Theseus and labyrinth
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of size n × m and
consists of blocks of size 1 × 1.
Each block of the labyrinth has a button that rotates all blocks 90 degrees
clockwise. Each block rotates around its center and doesn't change its position in the labyrinth. Also, each block has some number of doors (possibly none). In one minute, Theseus can either push the button in order to rotate all the blocks 90 degrees
clockwise or pass to the neighbouring block. Theseus can go from block A to some neighbouring block B only
if block A has a door that leads to block B and
block B has a door that leads to block A.
Theseus found an entrance to labyrinth and is now located in block (xT, yT) —
the block in the row xT and
column yT.
Theseus know that the Minotaur is hiding in block (xM, yM) and
wants to know the minimum number of minutes required to get there.
Theseus is a hero, not a programmer, so he asks you to help him.
Input
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 1000) —
the number of rows and the number of columns in labyrinth, respectively.
Each of the following n lines contains m characters,
describing the blocks of the labyrinth. The possible characters are:
«+» means this block has 4 doors (one door
to each neighbouring block);
«-» means this block has 2 doors — to the
left and to the right neighbours;
«|» means this block has 2 doors — to the
top and to the bottom neighbours;
«^» means this block has 1 door — to the
top neighbour;
«>» means this block has 1 door — to the
right neighbour;
«<» means this block has 1 door — to the
left neighbour;
«v» means this block has 1 door — to the
bottom neighbour;
«L» means this block has 3 doors — to all
neighbours except left one;
«R» means this block has 3 doors — to all
neighbours except right one;
«U» means this block has 3 doors — to all
neighbours except top one;
«D» means this block has 3 doors — to all
neighbours except bottom one;
«*» means this block is a wall and has no doors.
Left, right, top and bottom are defined from representing labyrinth as a table,
where rows are numbered from 1 to n from
top to bottom and columns are numbered from 1 to m from
left to right.
Next line contains two integers — coordinates of the block (xT, yT) (1 ≤ xT ≤ n, 1 ≤ yT ≤ m),
where Theseus is initially located.
Last line contains two integers — coordinates of the block (xM, yM) (1 ≤ xM ≤ n, 1 ≤ yM ≤ m),
where Minotaur hides.
It's guaranteed that both the block where Theseus starts and the block where Minotaur is hiding have at least one door. Theseus and Minotaur may be initially located at the same block.
Output
If Theseus is not able to get to Minotaur, then print -1 in the only line of the output. Otherwise, print the minimum number of minutes required to get to the
block where Minotaur is hiding.
Examples
input
2 2 +* *U 1 1 2 2
output
-1
input
2 3 <>< ><> 1 1 2 1
output
4
Note
Assume that Theseus starts at the block (xT, yT) at
the moment 0.
题意:一张图,一堆点,几扇门,能否到终点,不能则输出-1。
思路:这题体现了审清题目是十分重要的...红字比较关键,一般(wo)的思维是1为底 n为顶,白白贡献了N次WA,还有坐标的XY轴绝对不能弄反了。不然就会出现这种莫名其妙的WA:
2(在机器上提交和在codeforces提交的结果不同,说到底还是程序写的不对,但不容易查错)
Time: 0 ms, memory: 21804 KB
Verdict: WRONG_ANSWER
Input
2 3 <>< ><> 1 1 2 1
Participant's output
293666487
Jury's answer
4
Checker comment
wrong answer expected '4', found '293666487'
剩下的就比较简单了,一次90°,可以转换为四张地图,也就是三维bfs。
其他要注意的点:1.转一次是全图都转,而不是仅仅所在的那块。
2.转动为顺时针90°,而并非逆时针。
3.起点和终点可能为同一个点。
4.俩块间必须相互连通才能移动。
#include<bits/stdc++.h>
using namespace std;
int n,m;
char mp[4][1005][1005]={0};
int hav[4][1005][1005]={0};
struct node{
int a;
int b;
int time;
int type;
friend bool operator < (node a,node b){
return a.time>b.time;
}
};
int bg1,bg2,ed1,ed2;
int dir[5][2]={{0,1},{1,0},{0,-1},{-1,0},{0,0}};
bool check(int x1,int y1,int di,int type){
if(di==4) return true;
if(di==1){
char c=mp[type][x1][y1];
char d=mp[type][x1+dir[di][0]][y1+dir[di][1]];
if(c=='+'||c=='|'||c=='v'||c=='L'||c=='R'||c=='U'){
if(d=='+'||d=='|'||d=='^'||d=='L'||d=='R'||d=='D'){
return true;
}
}
return false;
}
else if(di==0){
char c=mp[type][x1][y1];
char d=mp[type][x1+dir[di][0]][y1+dir[di][1]];
if(c=='+'||c=='-'||c=='>'||c=='L'||c=='D'||c=='U'){
if(d=='+'||d=='-'||d=='<'||d=='U'||d=='R'||d=='D'){
return true;
}
}
return false;
}
else if(di==3){
char c=mp[type][x1][y1];
char d=mp[type][x1+dir[di][0]][y1+dir[di][1]];
if(d=='+'||d=='|'||d=='v'||d=='L'||d=='R'||d=='U'){
if(c=='+'||c=='|'||c=='^'||c=='L'||c=='R'||c=='D'){
return true;
}
}
return false;
}
else if(di==2){
char c=mp[type][x1][y1];
char d=mp[type][x1+dir[di][0]][y1+dir[di][1]];
if(d=='+'||d=='-'||d=='>'||d=='L'||d=='D'||d=='U'){
if(c=='+'||c=='-'||c=='<'||c=='U'||c=='R'||c=='D'){
return true;
}
}
return false;
}
}
int bfs(int x,int y,int type){
if(bg1==ed1&&bg2==ed2) return 0;
node first,next;
first.a=x,first.b=y,first.time=0;
first.type=type;
priority_queue<node>que;
que.push(first);
int fx,fy,fz;
while(!que.empty()){
first=que.top();
que.pop();
for(int i=0;i<5;i++){
if(i!=4){
fx=dir[i][0]+first.a;
fy=dir[i][1]+first.b;
fz=first.type;
}
else{
fx=first.a;
fy=first.b;
fz=(first.type+1)%4;
}
if(fx>=0&&fx<n&&fy>=0&&fy<m&&check(first.a,first.b,i,fz)&&!hav[fz][fx][fy]){
if(fx==ed1-1&&fy==ed2-1){
return first.time+1;
}
else nex
a889
t.time=first.time+1;
next.a=fx;
next.b=fy;
next.type=fz;
hav[fz][fx][fy]=1;
que.push(next);
}
}
}
return -1;
}
int main(void){
#define _Araragi_Tsukihi
#ifndef _Araragi_Tsukihi
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",mp[0][i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
switch(mp[0][i][j]){
case '+':
for(int k=1;k<4;k++) mp[k][i][j]='+';
break;
case '-':
mp[1][i][j]=mp[3][i][j]='|';
mp[2][i][j]='-'; break;
case '|':
mp[1][i][j]=mp[3][i][j]='-';
mp[2][i][j]='|'; break;
case '^':
mp[1][i][j]='>'; mp[2][i][j]='v'; mp[3][i][j]='<';
break;
case '>':
mp[1][i][j]='v'; mp[2][i][j]='<'; mp[3][i][j]='^';
break;
case '<':
mp[1][i][j]='^'; mp[2][i][j]='>'; mp[3][i][j]='v';
break;
case 'v':
mp[1][i][j]='<'; mp[2][i][j]='^'; mp[3][i][j]='>';
break;
case 'L':
mp[1][i][j]='U'; mp[2][i][j]='R'; mp[3][i][j]='D';
break;
case 'R':
mp[1][i][j]='D'; mp[2][i][j]='L'; mp[3][i][j]='U';
break;
case 'U':
mp[1][i][j]='R'; mp[2][i][j]='D'; mp[3][i][j]='L';
break;
case 'D':
mp[1][i][j]='L'; mp[2][i][j]='U'; mp[3][i][j]='R';
break;
case '*':
for(int k=1;k<4;k++) mp[k][i][j]='*';
break;
}
}
scanf("%d%d%d%d",&bg1,&bg2,&ed1,&ed2);
printf("%d\n",bfs(bg1-1,bg2-1,0));
return 0;
}
相关文章推荐
- libzdb简介
- Leetcode_c++: Search a 2D Matrix (074)
- SpringBoot操作MongoDB实现分页查询
- C# 调用 C++ 在Release下重新生成的问题
- libzdb
- 在OS X 10.10系统上安装Navicat Premium中文破解版11.0.16教程
- java基础理解(重载与重写--方法与类的分类等)
- [SM] 奇迹元素
- 刷刷笔试题~~[字符串类编程]
- C++模板类继承中的Name Lookup
- hdu1160-最长上升子序列
- 动态实现顺序表
- POJ 3278 Catch That Cow
- Apache Torque的使用
- 机器学习(二)--- SVM学习:理论基础理解
- Android SharedPreference
- Android关于buildToolVersion与CompileSdkVersion的区别
- HTML5.dcloud.io-stream-app
- 【51CTO学院三周年】通往牛逼的路上,在意的只有远方
- angular controller的一些用法