#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int inf=1e9;
int dp[10][10][505][505];
char c[505][505];
vector<ii> dist[500*500+5];
bool vis[505][505];
vector<ii> AdjList[505][505];
int n,h,w;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
bool ok(int x, int y) {
return x>=1&&x<=h&&y>=1&&y<=w&&c[x][y]!='x';
}
int id(int x, int y) {
return (x-1)*w+y;
}
ii run(int x, int y, int id) {
//cout<<"go "<<x<<" "<<y<<" "<<id<<endl;
while(true) {
//cout<<x<<" "<<y<<endl;
if(c[x][y]=='C') id=(id+1)%4;
if(c[x][y]=='A') id=(id+3)%4;
int nx=x+dx[id];
int ny=y+dy[id];
if(!ok(nx,ny)) return ii(x,y);
x=nx; y=ny;
}
}
void bfs(int l, int r) {
//cout<<"bfs "<<l<<" "<<r<<endl;
for(int i=1 ; i<=h ; i++) {
for(int j=1 ; j<=w ; j++) {
vis[i][j]=0;
}
}
for(int i=0 ; i<=n*n+50 ; i++) {
dist[i].clear();
}
int mn=inf;
int mx=0;
for(int i=1 ; i<=h ; i++) {
for(int j=1 ; j<=w ; j++) {
if(dp[l][r][i][j]!=-1) {
//cout<<"dp "<<l<<" "<<r<<" "<<i<<" "<<j<<" = "<<dp[l][r][i][j]<<endl;
dist[dp[l][r][i][j]].push_back(ii(i,j));
mn=min(dp[l][r][i][j],mn);
mx=max(dp[l][r][i][j],mx);
}
}
}
for(int i=mn ; i<=mx ; i++) {
for(ii tmp: dist[i]) {
int x=tmp.first;
int y=tmp.second;
if(vis[x][y]) continue;
else vis[x][y]=1;
for(ii tmp1: AdjList[x][y]) {
int nx=tmp1.first; int ny=tmp1.second;
//cout<<"nx ny = "<<nx<<" "<<ny<<endl;
if(dp[l][r][nx][ny]>dp[l][r][x][y]+1||dp[l][r][nx][ny]==-1) {
dp[l][r][nx][ny]=dp[l][r][x][y]+1;
mx=max(i+1,mx);
dist[dp[l][r][nx][ny]].push_back(ii(nx,ny));
}
}
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
cin>>w>>h;
memset(dp,-1,sizeof(dp));
for(int i=1 ; i<=h ; i++) {
cin>>c[i]+1;
for(int j=1 ; j<=w ; j++) {
if(c[i][j]>='0'&&c[i][j]<='9') {
int x=c[i][j]-'0';
dp[x][x][i][j]=0;
}
}
}
for(int i=1 ; i<=h ; i++) {
for(int j=1 ; j<=w ; j++) {
for(int id=0 ; id<4 ; id++) {
AdjList[i][j].push_back(run(i,j,id));
}
}
}
for(int d=0 ; d<n ; d++) {
for(int l=1 ; l+d<=n ; l++) {
int r=l+d;
for(int mid=l ; mid<r ; mid++) {
for(int x=1 ; x<=h ; x++) {
for(int y=1 ; y<=w ; y++) {
if((dp[l][r][x][y]==-1||dp[l][r][x][y]>dp[l][mid][x][y]+dp[mid+1][r][x][y])&&(dp[l][mid][x][y]!=-1&&dp[mid+1][r][x][y]!=-1))
dp[l][r][x][y]=dp[l][mid][x][y]+dp[mid+1][r][x][y];
}
}
}
bfs(l,r);
}
}
int ans=inf;
for(int i=1 ; i<=h ; i++) {
for(int j=1 ; j<=w ; j++) {
if(dp[1][n][i][j]!=-1) ans=min(ans,dp[1][n][i][j]);
}
}
if(ans==inf) cout<<-1;
else cout<<ans;
}
Compilation message
robots.cpp: In function 'int main()':
robots.cpp:78:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
78 | cin>>c[i]+1;
| ~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
111928 KB |
Output is correct |
2 |
Correct |
56 ms |
111912 KB |
Output is correct |
3 |
Correct |
47 ms |
111892 KB |
Output is correct |
4 |
Correct |
63 ms |
111948 KB |
Output is correct |
5 |
Correct |
49 ms |
111896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
111928 KB |
Output is correct |
2 |
Correct |
56 ms |
111912 KB |
Output is correct |
3 |
Correct |
47 ms |
111892 KB |
Output is correct |
4 |
Correct |
63 ms |
111948 KB |
Output is correct |
5 |
Correct |
49 ms |
111896 KB |
Output is correct |
6 |
Correct |
45 ms |
111924 KB |
Output is correct |
7 |
Correct |
48 ms |
111944 KB |
Output is correct |
8 |
Correct |
48 ms |
111892 KB |
Output is correct |
9 |
Correct |
60 ms |
111984 KB |
Output is correct |
10 |
Correct |
48 ms |
111956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
111928 KB |
Output is correct |
2 |
Correct |
56 ms |
111912 KB |
Output is correct |
3 |
Correct |
47 ms |
111892 KB |
Output is correct |
4 |
Correct |
63 ms |
111948 KB |
Output is correct |
5 |
Correct |
49 ms |
111896 KB |
Output is correct |
6 |
Correct |
45 ms |
111924 KB |
Output is correct |
7 |
Correct |
48 ms |
111944 KB |
Output is correct |
8 |
Correct |
48 ms |
111892 KB |
Output is correct |
9 |
Correct |
60 ms |
111984 KB |
Output is correct |
10 |
Correct |
48 ms |
111956 KB |
Output is correct |
11 |
Incorrect |
397 ms |
139600 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
111928 KB |
Output is correct |
2 |
Correct |
56 ms |
111912 KB |
Output is correct |
3 |
Correct |
47 ms |
111892 KB |
Output is correct |
4 |
Correct |
63 ms |
111948 KB |
Output is correct |
5 |
Correct |
49 ms |
111896 KB |
Output is correct |
6 |
Correct |
45 ms |
111924 KB |
Output is correct |
7 |
Correct |
48 ms |
111944 KB |
Output is correct |
8 |
Correct |
48 ms |
111892 KB |
Output is correct |
9 |
Correct |
60 ms |
111984 KB |
Output is correct |
10 |
Correct |
48 ms |
111956 KB |
Output is correct |
11 |
Incorrect |
397 ms |
139600 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |