QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#541043 | #9224. Express Eviction | FreeTimeLove | WA | 2ms | 4056kb | C++14 | 4.0kb | 2024-08-31 18:29:31 | 2024-08-31 18:29:31 |
Judging History
answer
/*FreeTimeLove's code.
Love has a nasty habit of disappearing over night.*/
#include<bits/stdc++.h>
namespace FRTMLV{
#define ll long long
#define LD long double
#define i7 __int128
#define re return
#define con continue
using namespace std;
template<class T>inline bool ckmin(T &a,T b){re b<a?a=b,1:0;}
template<class T>inline bool ckmax(T &a,T b){re a<b?a=b,1:0;}
const int N=55;
inline int rd(){
int ans=0,f=0;
char ch=getchar();
while(ch<'0'||ch>'9')f^=(ch=='-'),ch=getchar();
while(ch>='0'&&ch<='9')ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
re f?-ans:ans;
}
int n,m;
int nxt[4][2]={{1,1},{1,-1},{-1,1},{-1,-1}};
int nx[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int vis[N][N][4],bk[N][N],d[N][N];
char s[N][N];
struct node{
int x,y,d;
bool operator <(const node &a)const{re d>a.d;}
};
int cani[N][N];
bool bfs(int sx,int sy,int ex,int ey){
queue<node>q;
memset(d,0,sizeof d);
d[sx][sy]=1;q.push({sx,sy,0});
int ans=0;
cani[sx][sy]=cani[sx+1][sy]=cani[sx][sy+1]=cani[sx+1][sy+1]=1;
cani[ex][ey]=cani[ex+1][ey]=cani[ex][ey+1]=cani[ex+1][ey+1]=1;
while(!ans&&q.size()){
node p=q.front();q.pop();
for(int i=0;i<4;i++){
int x=p.x+nx[i][0],y=p.y+nx[i][1];
if(x>=0&&x<=n&&y>=0&&y<=m){
if(s[x][y]=='#'&&!cani[x][y])con;
if(s[x+1][y]=='#'&&!cani[x+1][y])con;
if(s[x][y+1]=='#'&&!cani[x][y+1])con;
if(s[x+1][y+1]=='#'&&!cani[x+1][y+1])con;
if(x==ex&&y==ey){ans=1;break;}
if(!d[x][y])q.push({x,y,0}),d[x][y]=1;
}
}
}
cani[sx][sy]=cani[sx+1][sy]=cani[sx][sy+1]=cani[sx+1][sy+1]=0;
cani[ex][ey]=cani[ex+1][ey]=cani[ex][ey+1]=cani[ex+1][ey+1]=0;
re ans;
}
int main(){
n=rd(),m=rd();
for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++){
for(int k=0;k<4;k++){
int x=i+nxt[k][0],y=j+nxt[k][1];
if(x>=0&&x<=n&&y>=0&&y<=m)vis[i][j][k]=bfs(i,j,x,y);
}
}
priority_queue<node>q;
memset(d,0x3f,sizeof d),d[0][0]=0;
q.push({0,0,0});
while(q.size()){
node p=q.top();q.pop();
if(bk[p.x][p.y])con;
bk[p.x][p.y]=1;
int x,y,nwd;
x=p.x+1,y=p.y+1;
if(vis[p.x][p.y][0]){
nwd=p.d+(s[x][y+1]=='#')+(s[x+1][y]=='#')+(s[x+1][y+1]=='#');
if(ckmin(d[x][y],nwd)&&!bk[x][y])q.push({x,y,nwd});
}
x=p.x+1,y=p.y-1;
if(vis[p.x][p.y][1]){
nwd=p.d+(s[x][y]=='#')+(s[x+1][y]=='#')+(s[x+1][y+1]=='#');
if(ckmin(d[x][y],nwd)&&!bk[x][y])q.push({x,y,nwd});
}
x=p.x-1,y=p.y+1;
if(vis[p.x][p.y][2]){
nwd=p.d+(s[x][y+1]=='#')+(s[x][y]=='#')+(s[x+1][y+1]=='#');
if(ckmin(d[x][y],nwd)&&!bk[x][y])q.push({x,y,nwd});
}
x=p.x-1,y=p.y-1;
if(vis[p.x][p.y][3]){
nwd=p.d+(s[x][y+1]=='#')+(s[x][y]=='#')+(s[x+1][y]=='#');
if(ckmin(d[x][y],nwd)&&!bk[x][y])q.push({x,y,nwd});
}
// for(int i=0;i<4;i++){
// if(vis[p.x][p.y][i]){
// int x=p.x+nxt[i][0],y=p.y+nxt[i][1];
// if(ckmin(d[x][y],p.d)&&!bk[x][y])q.push({x,y,p.d});
// }
// }
x=p.x+1,y=p.y;
if(x<=n){
nwd=p.d+(s[x+1][y]=='#')+(s[x+1][y+1]=='#');
if(ckmin(d[x][y],nwd)&&!bk[x][y])q.push({x,y,nwd});
}
x=p.x-1,y=p.y;
if(x>=0){
nwd=p.d+(s[x][y]=='#')+(s[x][y+1]=='#');
if(ckmin(d[x][y],nwd)&&!bk[x][y])q.push({x,y,nwd});
}
x=p.x,y=p.y+1;
if(y<=m){
nwd=p.d+(s[x][y+1]=='#')+(s[x+1][y+1]=='#');
if(ckmin(d[x][y],nwd)&&!bk[x][y])q.push({x,y,nwd});
}
x=p.x,y=p.y-1;
if(y>=0){
nwd=p.d+(s[x][y]=='#')+(s[x+1][y]=='#');
if(ckmin(d[x][y],nwd)&&!bk[x][y])q.push({x,y,nwd});
}
}
// for(int i=0;i<=n;i++){
// for(int j=0;j<=m;j++)printf("%d ",d[i][j]);
// puts("");
// }
// puts("");
// for(int i=0;i<=n;i++){
// for(int j=0;j<=m;j++){
// printf("(");
// for(int k=0;k<4;k++)printf("%d ",vis[i][j][k]);
// printf(")");
// }
// puts("");
// }
printf("%d\n",d[n][m]+(s[1][1]=='#'));
re 0;
}
/*
#########
#########
#########
#########
#########
#########
#########
2 2
##
.#
2 2
##
##
4 6
.##...
.#....
##....
....#.
*/
}int main(){re FRTMLV::main();}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4056kb
input:
4 6 .##... .#.... ##.... ....#.
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3880kb
input:
20 30 ...########################### #..########################### ...########################### ...########################### .############################# ...########################### ...########################### #..########################### ...########################### ..#...............
output:
11
result:
ok 1 number(s): "11"
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 3848kb
input:
35 35 ....###########...#########........ ##..#######################..#####. ....#######################..#####. ...#.....##################..#####. .##......##################.....##. .##..##..#############.....#....##. .....##..#############......##..##. ....#....#############..##..##..##. ####.....
output:
21
result:
wrong answer 1st numbers differ - expected: '16', found: '21'