답안 #541140

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
541140 2022-03-22T11:23:23 Z jiahng 로봇 (APIO13_robots) C++14
0 / 100
75 ms 163840 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define ll int
//~ #define int ll
typedef pair<ll, ll> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxh 510
#define maxn 10
#define INF 1e9
#define MOD 998244353
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;

int N,H,W;
char A[maxh][maxh];
pi dp[maxh][maxh][4];
int vis[maxh][maxh][4];
pi pos[maxn];
int dist[maxh][maxh];

int dr[] = {-1,0,1,0}; //clockwise from up
int dc[] = {0,1,0,-1};

pi dfs(int i,int j,int k){
	//~ cout << i << ' ' << j << ' ' << k << '\n';
	if (vis[i][j][k] == 2) return dp[i][j][k];
	if (vis[i][j][k] == 1){
		vis[i][j][k] = 2;
		return dp[i][j][k] = pi(-1,-1);
	}
	vis[i][j][k] = 1;
	int nk = k;
	if (A[i][j] == 'A') (nk+=3) %= 4;
	if (A[i][j] == 'C') (nk+=1) %= 4;
	
	int nr = i + dr[nk];
	int nc = j + dc[nk];
	
	if (nr < 1 || nr > H || nc < 1 || nc > W || A[nr][nc] == 'x') dp[i][j][k] = pi(i,j);
	else dp[i][j][k] = dfs(nr, nc, nk);
	vis[i][j][k] = 2;
	return dp[i][j][k];
}

int dp2[maxn][maxn][maxh][maxh];
queue <pi> Q[maxh*maxh];
vpi adj[maxh][maxh];

int32_t main(){
    fast;
    
    cin >> N >> W >> H;
    FOR(i,1,H) FOR(j,1,W) cin >> A[i][j];
    
    // find where each (i,j,k) goes to
    FOR(i,1,H) FOR(j,1,W) FOR(k,0,3) dp[i][j][k] = pi(-1,-1);
    FOR(i,1,H) FOR(j,1,W) FOR(k,0,3) if (vis[i][j][k] != 2) dfs(i,j,k);
	cout << 1;
	return 0;
    FOR(i,1,H) FOR(j,1,W) if (1 <= A[i][j]-'0' && A[i][j]-'0' <= N){
		pos[A[i][j]-'0'] = pi(i,j);
	}
	FOR(i,1,H) FOR(j,1,W) FOR(k,0,3){
		if (dp[i][j][k] != pi(-1,-1)) adj[i][j].pb(dp[i][j][k]); //dp[i][j][k].f][dp[i][j][k].s].pb(pi(i,j));
	}
	
	FOR(l,1,N) FOR(a,1,N){ 
		int b = a + l - 1; if (b > N) continue;
		FOR(x,1,H) FOR(y,1,W){
			dp2[a][b][x][y] = INF;
			FOR(c, a, b-1) dp2[a][b][x][y] = min(
				dp2[a][b][x][y], dp2[a][c][x][y] + dp2[c+1][b][x][y]);
				
			if (a == b && pi(x, y) == pos[a]) dp2[a][b][x][y] = 0;
		}
		
		FOR(x,1,H) FOR(y,1,W) if (dp2[a][b][x][y] <= H * W) Q[dp2[a][b][x][y]].push(pi(x,y));
		FOR(x,1,H) FOR(y,1,W) dist[x][y] = -1;
		FOR(d,0,H*W){
			while (!Q[d].empty()){
				pi cur = Q[d].front(); Q[d].pop();
				//~ cerr << a << ' ' << b << ' ' << cur.f << ' ' << cur.s << '\n';
				if (dist[cur.f][cur.s] != -1) continue;
				dist[cur.f][cur.s] = d;
				aFOR(x, adj[cur.f][cur.s]){
					if (dist[x.f][x.s] == -1) Q[d+1].push(x);
				}
			}
		}
		FOR(x,1,H) FOR(y,1,W) dp2[a][b][x][y] = (dist[x][y] == -1 ? INF : dist[x][y]);
		
	}
	//~ aFOR(i,adj[5][5]) cout << i.f << ' ' << i.s << '\n';
	//~ return 0;
	//~ cout << dp2[2][2][5][5];
	//~ return 0;
	int ans = INF;
	FOR(i,1,H) FOR(j,1,W) ans = min(ans, dp2[1][N][i][j]);
	cout << ans;
	
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 75 ms 163840 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 75 ms 163840 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 75 ms 163840 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 75 ms 163840 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -