CSES - BAPC 2015 - Results
Submission details
Task:Godzilla
Sender:KnowYourArchitecture
Submission time:2017-10-17 20:36:46 +0300
Language:C++
Status:READY
Result:
Test results
testverdicttime
#10.04 sdetails
#2ACCEPTED0.04 sdetails
#30.04 sdetails
#40.06 sdetails
#50.09 sdetails
#60.09 sdetails
#7ACCEPTED0.12 sdetails
#80.22 sdetails
#90.42 sdetails
#100.23 sdetails
#110.41 sdetails
#12ACCEPTED0.12 sdetails
#13ACCEPTED0.19 sdetails
#14ACCEPTED0.04 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:109:21: warning: 'Gy' may be used uninitialized in this function [-Wmaybe-uninitialized]
    if(lookup(Gy-1,Gx)=='R')--Gy,++r;
                     ^
input/code.cpp:15:8: warning: 'Gx' may be used uninitialized in this function [-Wmaybe-uninitialized]
    if(x<0||y<0||x>=L||y>=W)return (char)0;
        ^

Code

#include <bits/stdc++.h>
using namespace std;
int main(){
	int Tn;
	cin>>Tn;
	while(Tn--){
		// Read input
		int L,W;
		cin>>L>>W;
		vector<string> g(W);
		for(auto& it : g)cin>>it;

		// Function for getting value in g with bound checking
		auto lookup = [&](int y,int x){
			if(x<0||y<0||x>=L||y>=W)return (char)0;
			else return g[y][x];
		};

		// Queue for bfs for mechs
		queue<tuple<int,int>> qM;

		// What mechs can see
		// bit 0 means y-direction
		// bit 1 means x-direction
		// bit 2 means mech has visited
		vector<vector<int>> l(W);
		for(auto& it : l)it.resize(L);

		// Get value from mechs line-of-sight
		auto lookup_l = [&](int y,int x){
			if(x<0||y<0||x>=L||y>=W)return 4;
			else return l[y][x];
		};

		// Result, that is how many houses have been destroyed
		int r=0;

		// Extend beams horizontally and vertically
		auto update_beams = [&](int i,int j){
			// Check in y-direction
			if((lookup_l(i-1,j)&1) || (lookup_l(i+1,j)&1)){
				// Extend south
				for(int k=i;k<W;++k){
					if(lookup(k,j)=='R' || (lookup_l(k,j)&1))break;
					l[k][j]|=1;
				}
				// Extend north
				for(int k=i-1;k>=0;--k){
					if(lookup(k,j)=='R'|| (lookup_l(k,j)&1))break;
					l[k][j]|=1;
				}
			}
			// Check in x-direction
			if((lookup_l(i,j-1)&2) || (lookup_l(i,j+1)&2)){
				// Extend east
				for(int k=j;k<L;++k){
					if(lookup(i,k)=='R'||(lookup_l(i,k)&2))break;
					l[i][k]|=2;
				}
				// Extend west
				for(int k=j-1;k>=0;--k){
					if(lookup(i,k)=='R'||(lookup_l(i,k)&2))break;
					l[i][k]|=2;
				}
			}
		};
		auto cast_beam = [&](int i,int j){
			// Cast south
			for(int k=i;k<W;++k){
				if(lookup(k,j)=='R'||(lookup_l(k,j)&1))break;
				l[k][j]|=1;
			}
			// Cast north
			for(int k=i-1;k>=0;--k){
				if(lookup(k,j)=='R'||(lookup_l(k,j)&1))break;
				l[k][j]|=1;
			}
			// Cast east
			for(int k=j;k<L;++k){
				if(lookup(i,k)=='R'||(lookup_l(i,k)&2))break;
				l[i][k]|=2;
			}
			// Cast west
			for(int k=j-1;k>=0;--k){
				if(lookup(i,k)=='R'||(lookup_l(i,k)&2))break;
				l[i][k]|=2;
			}

		};

		// Position of gotzilla
		int Gx,Gy;
		for(int i=0;i<W;++i){
			for(int j=0;j<L;++j){
				// Add mech to bfs queue, cast beam and mark visited
				if(lookup(i,j)=='M') {
					qM.emplace(i,j);
					cast_beam(i,j);
					l[i][j]|=4;
				}
				// Get position of gotzilla
				if(lookup(i,j)=='G')Gx=j,Gy=i;
			}
		}

		while(true){
			// Update the position of gotzilla
			// First try moving on residental
			if(lookup(Gy-1,Gx)=='R')--Gy,++r;
			else if(lookup(Gy,Gx+1)=='R')++Gx,++r;
			else if(lookup(Gy+1,Gx)=='R')++Gy,++r;
			else if(lookup(Gy,Gx-1)=='R')--Gx,++r;
			// Then try moving on empty, non-visited cells
			else if(lookup(Gy-1,Gx)!='G'&&lookup(Gy-1,Gx))--Gy;
			else if(lookup(Gy,Gx+1)!='G'&&lookup(Gy,Gx+1))++Gx;
			else if(lookup(Gy+1,Gx)!='G'&&lookup(Gy+1,Gx))++Gy;
			else if(lookup(Gy,Gx-1)!='G'&&lookup(Gy,Gx-1))--Gx;

			// Mark visited
			g[Gy][Gx]='G';
			// Update beams
			update_beams(Gy,Gx);

			// Move all mechs
			int vv = qM.size();
			while(vv--){
				// Get the position of mech
				int i,j;
				tie(i,j) = qM.front();qM.pop();

				// Check that no mech has been there and it is not residental
				// Then cast beam from new postion, mark it as visited and add the new position to the queue
				if((~lookup_l(i+1,j)&4) && lookup_l(i+1,j) != 'R') cast_beam(i+1,j), l[i+1][j]|=4, qM.emplace(i+1,j);
				if((~lookup_l(i-1,j)&4) && lookup_l(i-1,j) != 'R') cast_beam(i-1,j), l[i-1][j]|=4, qM.emplace(i-1,j);
				if((~lookup_l(i,j+1)&4) && lookup_l(i,j+1) != 'R') cast_beam(i,j+1), l[i][j+1]|=4, qM.emplace(i,j+1);
				if((~lookup_l(i,j-1)&4) && lookup_l(i,j-1) != 'R') cast_beam(i,j-1), l[i][j-1]|=4, qM.emplace(i,j-1);
			}
			// If gozilla is in beam or with mech
			if(lookup_l(Gy,Gx))break;
		}
		// Output answer
		cout<<r<<'\n';
	}
}

Test details

Test 1

Verdict:

input
6
3 3
RR.
G..
M.R
...

correct output
1
2
2
21
0
...

user output
1
2
0
7
0
...

Test 2

Verdict: ACCEPTED

input
6
3 1000
...
...
...
...

correct output
0
4
0
1
1
...

user output
0
4
0
1
1
...

Test 3

Verdict:

input
5
83 83
..R....R........R.RRR.R..........

correct output
0
8
0
1
1

user output
0
6
0
1
1

Test 4

Verdict:

input
5
183 183
..R...R.............R.....R.R....

correct output
15
5
30
16
0

user output
11
5
25
16
0

Test 5

Verdict:

input
5
283 283
........................RR.......

correct output
0
31
4
2
13

user output
0
25
2
2
12

Test 6

Verdict:

input
5
383 383
R.R...R........R...........R.R...

correct output
5
2
68
6
14

user output
3
2
64
4
14

Test 7

Verdict: ACCEPTED

input
5
483 483
R.........R......................

correct output
1
2
7
20
2

user output
1
2
7
20
2

Test 8

Verdict:

input
5
583 583
.R...R.....R........R.....R......

correct output
5
5
86
1
51

user output
5
5
86
1
49

Test 9

Verdict:

input
5
683 683
R...............R...R...R........

correct output
24
136
18
4
12

user output
24
134
18
4
11

Test 10

Verdict:

input
5
783 783
..........R....R....R........R...

correct output
12
18
10
5
4

user output
9
3
10
3
4

Test 11

Verdict:

input
5
883 883
..RR.R........R.R................

correct output
33
68
58
29
26

user output
27
67
54
24
25

Test 12

Verdict: ACCEPTED

input
1
983 983
...R.R.R.R.....R.R.R.R...R.......

correct output
21

user output
21

Test 13

Verdict: ACCEPTED

input
1
1000 1000
..R...R..R......R..........R.....

correct output
23

user output
23

Test 14

Verdict: ACCEPTED

input
2
12 6
....R.....R.
.R........RG
.RRRRRRRRRR.
...

correct output
2
0

user output
2
0