Submission #306256


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, int A[55][55]) {
    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]+1 == L[yy][xx] && A[yy][xx]) {
	    ret.push_back( make_pair(yy, xx) );
	}
    }
    return ret;
}

int AZ[55][55], BZ[55][55];

int main() {
    scanf("%d%d", &H, &W);
    int sy, sx;

    REP (i, H) scanf("%s", F[i]);
    REP (i, H) {
	REP (j, W) {
	    if (F[i][j] == 'S') {
		sy = i;
		sx = j;
	    }
	}
    }

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

    path('A', AZ);
    path('B', BZ);

    int ax, ay, bx, by;
    int aex, aey, bex, bey;
    ax = bx = sx;
    ay = by = sy;
    REP (i, H) REP (j, W) {
	if (F[i][j] == 'A') {
	    aex = j;
	    aey = i;
	}
	if (F[i][j] == 'B') {
	    bex = j;
	    bey = i;
	}
    }

    if (L[aey][aex] > 9999 || L[bey][bex] > 9999) NA();

    while (true) {
	if (ax == aex && ay == aey) break;
	if (bx == bex && by == bey) break;
	vector<pair<int, int> >A = search(ay, ax, AZ);
	vector<pair<int, int> >B = search(by, bx, BZ);

	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();
    }

    while (ax != aex || ay != aey)  {
	vector<pair<int, int> >A = search(ay, ax, AZ);
	ay = A[0].first;
	ax = A[0].second;
	F[ay][ax] = 'a';
    }

    while (bx != bex || by != bey)  {
	vector<pair<int, int> >B = search(by, bx, BZ);
	by = B[0].first;
	bx = B[0].second;
	F[by][bx] = 'b';
    }

    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 3843 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:112: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 × 13
WA × 28
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 WA 23 ms 796 KB
manual_j10.txt AC 26 ms 784 KB
manual_j11.txt WA 24 ms 932 KB
manual_j12.txt WA 23 ms 920 KB
manual_j13.txt WA 23 ms 812 KB
manual_j14.txt AC 24 ms 800 KB
manual_j15.txt WA 23 ms 804 KB
manual_j2.txt WA 24 ms 800 KB
manual_j3.txt AC 23 ms 928 KB
manual_j4.txt WA 24 ms 924 KB
manual_j5.txt WA 24 ms 928 KB
manual_j6.txt AC 24 ms 800 KB
manual_j7.txt WA 29 ms 764 KB
manual_j8.txt WA 23 ms 796 KB
manual_j9.txt WA 24 ms 928 KB
manual_k1.txt WA 25 ms 928 KB
manual_k2.txt AC 24 ms 920 KB
random_01.txt AC 22 ms 920 KB
random_02.txt WA 23 ms 800 KB
random_03.txt WA 23 ms 808 KB
random_04.txt WA 24 ms 920 KB
random_05.txt WA 24 ms 800 KB
random_06.txt AC 26 ms 924 KB
random_max_01.txt WA 26 ms 796 KB
random_max_02.txt WA 25 ms 812 KB
random_max_03.txt AC 22 ms 800 KB
random_max_04.txt WA 22 ms 924 KB
random_max_05.txt WA 23 ms 796 KB
random_max_06.txt WA 25 ms 788 KB
random_max_07.txt WA 23 ms 924 KB
random_max_08.txt WA 22 ms 928 KB
random_max_09.txt AC 25 ms 804 KB
random_max_10.txt AC 22 ms 808 KB
sample_01.txt WA 24 ms 928 KB
sample_02.txt AC 23 ms 792 KB
sample_03.txt AC 23 ms 800 KB
sample_04.txt WA 22 ms 800 KB
white_01.txt WA 23 ms 932 KB
white_02.txt WA 24 ms 920 KB
white_03.txt WA 23 ms 796 KB
white_04.txt AC 23 ms 924 KB