Submission #19532898


Source Code Expand

Copy
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char mp[505][505];
int book[505][505];
int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};
int n, m, sx, sy, fx, fy, head = 1, tail = 2, midx, midy, ans,flag;
struct mm {
	int x;
	int y;
	int bushu;
}queue[1000005];
int main () {
    ios::sync_with_stdio(0);
	cin >> n >> m;
	for (register int i = 1; i <= n;i++) {
		for (register int j = 1; j <= m;j++) {
			cin >> mp[i][j];
			if (mp[i][j] == 'S') {
				sx = i;
				sy = j;
			}
			if (mp[i][j] == 'G') {
				fx = i;
				fy = j;
			}
			if (mp[i][j]=='C') {
				midx = i;
				midy = j;
			}
		}
	}
	queue[head].x = sx;
	queue[head].y = sy;
	queue[head].bushu = 0;
	book[sx][sy] = 1;
	while (head<tail) {
		flag=0;
		for (int i = 0; i < 4;i++) {
			int idx = queue[head].x + dx[i];
			int idy = queue[head].y + dy[i];
			if (idx>=1&&idx<=n&&idy>=0&&idy<=m&&book[idx][idy]==0&&mp[idx][idy]!='#') {
				book[idx][idy] = 1;
				queue[tail].x = idx;
				queue[tail].y = idy;
				queue[tail].bushu = queue[head].bushu + 1;
				tail++;
				if (idx==midx&&idy==midy) {
					ans = queue[tail-1].bushu;
					flag = 1;
					break;
				}
			}
		}
		if (flag==1) {
			break;
		}
		head++;
	}
	memset(queue, 0, sizeof(queue));
	memset(book,0,sizeof(book));
	if (flag==0) {
		cout<<"-1"<<endl;
		return 0;
	}
	flag=0;
	head = 1;
	tail = 2;
	queue[head].x = midx;
	queue[head].y = midy;
	queue[head].bushu = ans;
	while (head<tail){
		flag=0;
		for (int i = 0; i < 4;i++) {
			int idx = queue[head].x + dx[i];
			int idy = queue[head].y + dy[i];
			if (idx>=1&&idx<=n&&idy>=0&&idy<=m&&book[idx][idy]==0&&mp[idx][idy]!='#') {
				book[idx][idy] = 1;
				queue[tail].x = idx;
				queue[tail].y = idy;
				queue[tail].bushu = queue[head].bushu + 1;
				tail++;
				if (idx==fx&&idy==fy) {
					flag=1;
					cout<<queue[tail-1].bushu<<endl;
					return 0;
				}
			}
		}
		if (flag==1) {
			break;
		}
		head++;
	}
	if (flag==0) {
		cout<<"-1"<<endl;
	}
	return 0;
}

Submission Info

Submission Time
Task C - 自宅からの脱出
User luogu_bot2
Language C++ (GCC 9.2.1)
Score 100
Code Size 2075 Byte
Status AC
Exec Time 38 ms
Memory 16624 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:18:20: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
   18 |  for (register int i = 1; i <= n;i++) {
      |                    ^
./Main.cpp:19:21: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
   19 |   for (register int j = 1; j <= m;j++) {
      |                     ^

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 15 ms 16260 KB
input_1.txt AC 13 ms 16324 KB
input_10.txt AC 16 ms 16376 KB
input_11.txt AC 15 ms 16336 KB
input_12.txt AC 13 ms 16292 KB
input_13.txt AC 16 ms 16624 KB
input_14.txt AC 30 ms 16484 KB
input_15.txt AC 38 ms 16508 KB
input_16.txt AC 21 ms 16536 KB
input_17.txt AC 37 ms 16488 KB
input_18.txt AC 16 ms 16356 KB
input_19.txt AC 18 ms 16296 KB
input_2.txt AC 16 ms 16260 KB
input_20.txt AC 18 ms 16300 KB
input_21.txt AC 32 ms 16500 KB
input_22.txt AC 22 ms 16624 KB
input_23.txt AC 28 ms 16552 KB
input_3.txt AC 23 ms 16208 KB
input_4.txt AC 15 ms 16264 KB
input_5.txt AC 18 ms 16208 KB
input_6.txt AC 15 ms 16376 KB
input_7.txt AC 17 ms 16364 KB
input_8.txt AC 15 ms 16240 KB
input_9.txt AC 14 ms 16256 KB