What is the Codeforces Round #XXX problem set like?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1323个文字,预计阅读时间需要6分钟。
D.+忒修斯与迷宫+D.+忒修斯与迷宫时间限制+每测试3秒+内存限制+每测试256兆字节+输入+标准输入+输出+标准输出+忒修斯刚抵达克里特岛,准备与牛头怪战斗。他发现了一个迷宫……
D. Theseus and labyrinth
栏
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 sizen × mand consists of blocks of size1 × 1.
Eachblock of the labyrinth has a button that rotatesallblocks90degrees 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 blocks90degrees clockwise or pass to the neighbouring block. Theseus can go from blockAto some neighbouring blockBonly if blockAhas a door that leads to blockBand blockBhas a door that leads to blockA.
Theseus found an entrance to labyrinth and is now located in block(xT, yT)— the block in the rowxTand columnyT. Theseus know that the Minotaur is hiding in block(xM, yM)
Theseus is a hero, not a programmer, so he asks you to help him.
Input
The first line of the input contains two integersnandm(1 ≤ n, m ≤ 1000)— the number of rows and the number of columns in labyrinth, respectively.
Each of the followingnlines containsm
- «+» means this block has4
- «-» means this block has2
- «|» means this block has2
- «^» means this block has1
- «>» means this block has1
- «<» means this block has1
- «v» means this block has1
- «L» means this block has3
- «R» means this block has3
- «U» means this block has3
- «D» means this block has3
- «*» 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 from1tonfrom top to bottom and columns are numbered from1tom
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
Examples
input
+*
*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 moment0.
题解;
在一幅n*m的地图,地图上有很多标志,然后每秒钟可以穿过门,也可以使得所有门都顺时针转动90°
如果你要从A到B,那么从B也必须能够到达A,问从起点到终点的最短时间是多少?
BFS撸一波。。
AC代码:
#include<bits/stdc++.h>using namespace std;
typedef long long ll;
const int N=1000+5;
const int inf=1e9;
char s[4][N][N];
int d[4][N][N],n,m;
bool vis[4][N][N];
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
bool go(int i,int j,int k,int z){
switch(s[z][i][j]){
case '+':return true;
case '-':return k==0||k==2;
case '|':return k==1||k==3;
case '^':return k==3;
case '>':return k==0;
case '<':return k==2;
case 'v':return k==1;
case 'L':return k!=2;
case 'R':return k!=0;
case 'U':return k!=3;
case 'D':return k!=1;
case '*':return false;
}
}
int cor(int k){
return k<=1?k+2:k-2;
}
bool check(int i,int j,int k,int z){
int x=i+dx[k],y=j+dy[k];
if(x<1||x>n||y<1||y>m||vis[z][x][y])return false;
return go(i,j,k,z)&&go(x,y,cor(k),z);
}
void bfs(int x,int y){
queue<int>qx,qy,q;
qx.push(x);
qy.push(y);
q.push(0);
vis[0][x][y]=1;
while(!q.empty()){
x=qx.front(); y=qy.front();
int z=q.front();
qx.pop();
qy.pop();
q.pop();
for(int i=0;i<=3;i++)
if(check(x,y,i,z)){
int xx=x+dx[i],yy=y+dy[i];
qx.push(xx);
qy.push(yy);
q.push(z);
d[z][xx][yy]=d[z][x][y]+1;
vis[z][xx][yy]=1;
}
if(!vis[(z+1)%4][x][y]){
d[(z+1)%4][x][y]=d[z][x][y]+1;
vis[(z+1)%4][x][y]=1;
qx.push(x);
qy.push(y);
q.push((z+1)%4);
}
}
}
char change(int i,int j,int k){
switch(s[k-1][i][j]){
case '+':return '+';
case '-':return '|';
case '|':return '-';
case '^':return '>';
case '>':return 'v';
case 'v':return '<';
case '<':return '^';
case 'L':return 'U';
case 'U':return 'R';
case 'R':return 'D';
case 'D':return 'L';
case '*':return '*';
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",s[0][i]+1);
for(int k=1;k<=3;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
s[k][i][j]=change(i,j,k);
int x,y; scanf("%d%d",&x,&y);
bfs(x,y);
int ans=inf;
scanf("%d%d",&x,&y);
for(int i=0;i<=3;i++)
if(vis[i][x][y])
ans=min(ans,d[i][x][y]);
if(ans==inf)ans=-1;
printf("%d\n",ans);
return 0;
}
本文共计1323个文字,预计阅读时间需要6分钟。
D.+忒修斯与迷宫+D.+忒修斯与迷宫时间限制+每测试3秒+内存限制+每测试256兆字节+输入+标准输入+输出+标准输出+忒修斯刚抵达克里特岛,准备与牛头怪战斗。他发现了一个迷宫……
D. Theseus and labyrinth
栏
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 sizen × mand consists of blocks of size1 × 1.
Eachblock of the labyrinth has a button that rotatesallblocks90degrees 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 blocks90degrees clockwise or pass to the neighbouring block. Theseus can go from blockAto some neighbouring blockBonly if blockAhas a door that leads to blockBand blockBhas a door that leads to blockA.
Theseus found an entrance to labyrinth and is now located in block(xT, yT)— the block in the rowxTand columnyT. Theseus know that the Minotaur is hiding in block(xM, yM)
Theseus is a hero, not a programmer, so he asks you to help him.
Input
The first line of the input contains two integersnandm(1 ≤ n, m ≤ 1000)— the number of rows and the number of columns in labyrinth, respectively.
Each of the followingnlines containsm
- «+» means this block has4
- «-» means this block has2
- «|» means this block has2
- «^» means this block has1
- «>» means this block has1
- «<» means this block has1
- «v» means this block has1
- «L» means this block has3
- «R» means this block has3
- «U» means this block has3
- «D» means this block has3
- «*» 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 from1tonfrom top to bottom and columns are numbered from1tom
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
Examples
input
+*
*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 moment0.
题解;
在一幅n*m的地图,地图上有很多标志,然后每秒钟可以穿过门,也可以使得所有门都顺时针转动90°
如果你要从A到B,那么从B也必须能够到达A,问从起点到终点的最短时间是多少?
BFS撸一波。。
AC代码:
#include<bits/stdc++.h>using namespace std;
typedef long long ll;
const int N=1000+5;
const int inf=1e9;
char s[4][N][N];
int d[4][N][N],n,m;
bool vis[4][N][N];
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
bool go(int i,int j,int k,int z){
switch(s[z][i][j]){
case '+':return true;
case '-':return k==0||k==2;
case '|':return k==1||k==3;
case '^':return k==3;
case '>':return k==0;
case '<':return k==2;
case 'v':return k==1;
case 'L':return k!=2;
case 'R':return k!=0;
case 'U':return k!=3;
case 'D':return k!=1;
case '*':return false;
}
}
int cor(int k){
return k<=1?k+2:k-2;
}
bool check(int i,int j,int k,int z){
int x=i+dx[k],y=j+dy[k];
if(x<1||x>n||y<1||y>m||vis[z][x][y])return false;
return go(i,j,k,z)&&go(x,y,cor(k),z);
}
void bfs(int x,int y){
queue<int>qx,qy,q;
qx.push(x);
qy.push(y);
q.push(0);
vis[0][x][y]=1;
while(!q.empty()){
x=qx.front(); y=qy.front();
int z=q.front();
qx.pop();
qy.pop();
q.pop();
for(int i=0;i<=3;i++)
if(check(x,y,i,z)){
int xx=x+dx[i],yy=y+dy[i];
qx.push(xx);
qy.push(yy);
q.push(z);
d[z][xx][yy]=d[z][x][y]+1;
vis[z][xx][yy]=1;
}
if(!vis[(z+1)%4][x][y]){
d[(z+1)%4][x][y]=d[z][x][y]+1;
vis[(z+1)%4][x][y]=1;
qx.push(x);
qy.push(y);
q.push((z+1)%4);
}
}
}
char change(int i,int j,int k){
switch(s[k-1][i][j]){
case '+':return '+';
case '-':return '|';
case '|':return '-';
case '^':return '>';
case '>':return 'v';
case 'v':return '<';
case '<':return '^';
case 'L':return 'U';
case 'U':return 'R';
case 'R':return 'D';
case 'D':return 'L';
case '*':return '*';
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",s[0][i]+1);
for(int k=1;k<=3;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
s[k][i][j]=change(i,j,k);
int x,y; scanf("%d%d",&x,&y);
bfs(x,y);
int ans=inf;
scanf("%d%d",&x,&y);
for(int i=0;i<=3;i++)
if(vis[i][x][y])
ans=min(ans,d[i][x][y]);
if(ans==inf)ans=-1;
printf("%d\n",ans);
return 0;
}

