Submission #306146


Source Code Expand

#include<queue>
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &a, const T &b) { if (a>b) a=b; }
template<class T> inline void amax(T &a, const T &b) { if (a<b) a=b; }

const int dx[] = {0,1,0,-1};
const int dy[] = {1,0,-1,0};

void NA() {
    puts("NA");
    exit(0);
}

int H, W;
char F[55][55];
int L[55][55], fX[55][55], fY[55][55];

bool in(int y, int x) {
    return 0 <= x && x < W && 0 <= y && y < H;
}

void solve() {
    int sx, sy;
    REP (i, H) REP (j, W) {
	if (F[i][j] == 'S') {
	    sx = j;
	    sy = i;
	}
    }
    memset(L, 0x3f, sizeof L);
    memset(fX, -1, sizeof fX);
    memset(fY, -1, sizeof fY);

    queue<int> qu;
    qu.push(sx); qu.push(sy);
    L[sy][sx] = 0;
    while (qu.size()) {
	int x = qu.front(); qu.pop();
	int y = qu.front(); qu.pop();
	REP (d, 4) {
	    int xx = x + dx[d];
	    int yy = y + dy[d];
	    if (0 <= xx && xx < W && 0 <= yy && yy < H &&
		    F[yy][xx] != '#' &&
		    L[yy][xx] > L[y][x] + 1) {
		L[yy][xx] = L[y][x] + 1;
		qu.push(xx);
		qu.push(yy);
	    }
	}
    }
}

void path(char G, int C[55][55]) {
    int sx, sy;
    REP (i, H) REP (j, W) {
	if (F[i][j] == G) {
	    sx = j;
	    sy = i;
	}
    }
    queue<int> qu;
    qu.push(sx); qu.push(sy);
    C[sy][sx] = 1;
    while (qu.size()) {
	int x = qu.front(); qu.pop();
	int y = qu.front(); qu.pop();
	REP (d, 4) {
	    int xx = x + dx[d];
	    int yy = y + dy[d];
	    if (0 <= xx && xx < W && 0 <= yy && yy < H &&
		    L[yy][xx] == L[y][x] - 1 && C[yy][xx] == 0) {
		C[yy][xx] = 1;
		qu.push(xx);
		qu.push(yy);
	    }
	}
    }
}

vector<pair<int, int> > search(int y, int x) {
    vector<pair<int, int> > ret;
    for (int d=0; d<4; ++d) {
	int xx = x + dx[d];
	int yy = y + dy[d];
	if (in(yy, xx) && L[y][x] > L[yy][xx] && F[yy][xx] == '.') {
	    ret.push_back( make_pair(yy, xx) );
	}
    }
    return ret;
}

// int A[55][55], B[55][55];

int main() {
    scanf("%d%d", &H, &W);
    REP (i, H) scanf("%s", F[i]);

    solve();
//    REP (i, H) {
//	REP (j, W) {
//	    printf("%d ", L[i][j]);
//	}
//	puts("");
//    }

//    path('A', A);
//    path('B', B);
//
    int ax, ay, bx, by;
    REP (i, H) REP (j, W) {
	if (F[i][j] == 'A') {
	    ax = j;
	    ay = i;
	}
	if (F[i][j] == 'B') {
	    bx = j;
	    by = i;
	}
    }

    while (true) {
	if (L[ay][ax] < L[by][bx]) {
	    vector<pair<int, int> >P = search(ay, ax);
	    if (P.size() == 0u) NA();
	    ay = P[0].first;
	    ax = P[0].second;
	    F[ay][ax] = 'a';
	} else if (L[by][bx] < L[ay][ax]) {
	    vector<pair<int, int> >P = search(ay, ax);
	    if (P.size() == 0u) NA();
	    by = P[0].first;
	    bx = P[0].second;
	    F[by][bx] = 'b';
	} else {
	    if (L[ay][ax] == 1) break;
	    vector<pair<int, int> >A = search(ay, ax);
	    vector<pair<int, int> >B = search(by, bx);

	    bool ok = false;
	    REP (i, A.size()) {
		REP (j, B.size()) {
		    if (A[i] != B[j]) {
			ok = true;
			ay = A[i].first;
			ax = A[i].second;
			F[ay][ax] = 'a';
			by = B[j].first;
			bx = B[j].second;
			F[by][bx] = 'b';
			break;
		    }
		}
		if (ok) break;
	    }
	    if (!ok) NA();
	}
    }

    REP (i, H) puts(F[i]);

    return 0;
}

Submission Info

Submission Time
Task D - Maze
User natsugiri
Language C++ (G++ 4.6.4)
Score 0
Code Size 3612 Byte
Status WA
Exec Time 29 ms
Memory 932 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:109:26: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
./Main.cpp:110:33: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]

Judge Result

Set Name all
Score / Max Score 0 / 100
Status
AC × 19
WA × 22
Set Name Test Cases
all manual_j1.txt, manual_j10.txt, manual_j11.txt, manual_j12.txt, manual_j13.txt, manual_j14.txt, manual_j15.txt, manual_j2.txt, manual_j3.txt, manual_j4.txt, manual_j5.txt, manual_j6.txt, manual_j7.txt, manual_j8.txt, manual_j9.txt, manual_k1.txt, manual_k2.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_max_01.txt, random_max_02.txt, random_max_03.txt, random_max_04.txt, random_max_05.txt, random_max_06.txt, random_max_07.txt, random_max_08.txt, random_max_09.txt, random_max_10.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, white_01.txt, white_02.txt, white_03.txt, white_04.txt
Case Name Status Exec Time Memory
manual_j1.txt AC 24 ms 924 KB
manual_j10.txt AC 23 ms 796 KB
manual_j11.txt WA 22 ms 796 KB
manual_j12.txt AC 22 ms 920 KB
manual_j13.txt AC 22 ms 932 KB
manual_j14.txt AC 24 ms 924 KB
manual_j15.txt WA 24 ms 924 KB
manual_j2.txt WA 24 ms 800 KB
manual_j3.txt AC 24 ms 928 KB
manual_j4.txt WA 24 ms 928 KB
manual_j5.txt WA 24 ms 924 KB
manual_j6.txt AC 24 ms 924 KB
manual_j7.txt WA 25 ms 800 KB
manual_j8.txt AC 24 ms 928 KB
manual_j9.txt WA 24 ms 916 KB
manual_k1.txt WA 24 ms 920 KB
manual_k2.txt AC 24 ms 928 KB
random_01.txt AC 24 ms 928 KB
random_02.txt WA 25 ms 920 KB
random_03.txt WA 24 ms 928 KB
random_04.txt WA 23 ms 924 KB
random_05.txt WA 24 ms 796 KB
random_06.txt AC 22 ms 928 KB
random_max_01.txt WA 24 ms 928 KB
random_max_02.txt WA 24 ms 924 KB
random_max_03.txt AC 23 ms 920 KB
random_max_04.txt WA 24 ms 800 KB
random_max_05.txt WA 24 ms 796 KB
random_max_06.txt WA 22 ms 932 KB
random_max_07.txt WA 24 ms 932 KB
random_max_08.txt WA 29 ms 924 KB
random_max_09.txt AC 25 ms 920 KB
random_max_10.txt AC 23 ms 916 KB
sample_01.txt AC 23 ms 916 KB
sample_02.txt AC 23 ms 800 KB
sample_03.txt AC 24 ms 920 KB
sample_04.txt AC 23 ms 916 KB
white_01.txt WA 26 ms 732 KB
white_02.txt WA 24 ms 924 KB
white_03.txt WA 24 ms 924 KB
white_04.txt AC 22 ms 928 KB