Submission #19543076


Source Code Expand

Copy
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;

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

inline int bfs(int sy, int sx, int gy, int gx, string s[])
{
	queue<P> q;
	q.push({sy, sx});
	vector<vector<int> > dist(n, vector<int>(m, -1));
	dist[sy][sx] = 0;
	while (!q.empty()) {
		int x, y;
		tie(y, x) = q.front(); q.pop();
		REP(i, 4) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if (s[ny][nx] == '#') continue ;
			if (nx < 0 || m <= nx || ny < 0 || n <= ny) continue ;
			if (dist[ny][nx] != -1) continue ;
			dist[ny][nx] = dist[y][x] + 1;
			q.push({ny, nx});
		}
	}
	return dist[gy][gx];
}

int main() {
	cin >> n >> m;
	string s[n];
	REP(i, n) cin >> s[i];
	int sx, sy, cx, cy, gx, gy;
	REP(i, n) REP(j, m) {
		if (s[i][j] == 'S') sy = i, sx = j;
		if (s[i][j] == 'C') cy = i, cx = j;
		if (s[i][j] == 'G') gy = i, gx = j;
	}
	int res1 = bfs(sy, sx, cy, cx, s);
	int res2 = bfs(cy, cx, gy, gx, s);
	if (res1 == -1 || res2 == -1) cout << -1 << '\n';
	else cout << res1 + res2 << '\n';
	return 0;
}

Submission Info

Submission Time
Task C - 自宅からの脱出
User solareenlo
Language C++ (GCC 9.2.1)
Score 100
Code Size 1160 Byte
Status AC
Exec Time 30 ms
Memory 5016 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:43:16: warning: ‘gy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   43 |  int res2 = bfs(cy, cx, gy, gx, s);
      |             ~~~^~~~~~~~~~~~~~~~~~~
./Main.cpp:43:16: warning: ‘gx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
./Main.cpp:42:16: warning: ‘cy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   42 |  int res1 = bfs(sy, sx, cy, cx, s);
      |             ~~~^~~~~~~~~~~~~~~~~~~
./Main.cpp:42:16: warning: ‘cx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
./Main.cpp:42:16: warning: ‘sy’ may be used uninitialized in this function [-Wmaybe-uninitialized]
./Main.cpp:42:16: warning: ‘sx’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 24
Set Name Test Cases
All input_0.txt, input_1.txt, input_10.txt, input_11.txt, input_12.txt, input_13.txt, input_14.txt, input_15.txt, input_16.txt, input_17.txt, input_18.txt, input_19.txt, input_2.txt, input_20.txt, input_21.txt, input_22.txt, input_23.txt, input_3.txt, input_4.txt, input_5.txt, input_6.txt, input_7.txt, input_8.txt, input_9.txt
Case Name Status Exec Time Memory
input_0.txt AC 6 ms 3576 KB
input_1.txt AC 3 ms 3600 KB
input_10.txt AC 2 ms 3520 KB
input_11.txt AC 2 ms 3396 KB
input_12.txt AC 3 ms 3596 KB
input_13.txt AC 3 ms 3584 KB
input_14.txt AC 22 ms 4820 KB
input_15.txt AC 27 ms 4940 KB
input_16.txt AC 10 ms 4568 KB
input_17.txt AC 30 ms 5016 KB
input_18.txt AC 8 ms 3600 KB
input_19.txt AC 4 ms 3512 KB
input_2.txt AC 2 ms 3580 KB
input_20.txt AC 3 ms 3536 KB
input_21.txt AC 27 ms 4972 KB
input_22.txt AC 13 ms 4672 KB
input_23.txt AC 14 ms 4776 KB
input_3.txt AC 6 ms 3484 KB
input_4.txt AC 2 ms 3596 KB
input_5.txt AC 3 ms 3500 KB
input_6.txt AC 2 ms 3576 KB
input_7.txt AC 3 ms 3536 KB
input_8.txt AC 3 ms 3540 KB
input_9.txt AC 4 ms 3592 KB