-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path1098-Robots on Ice.cpp
72 lines (68 loc) · 2.16 KB
/
1098-Robots on Ice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <bits/stdc++.h>
using namespace std;
int n,m,total,tc=1;
vector<pair<int,int>> t;
vector<int> checkpoints;
vector<vector<bool>> visited,visited2;
vector<vector<int>> dirs = {{1,0},{0,1},{-1,0},{0,-1}};
// count reachable
int dfs2(int x, int y){
if(x<0||y<0||x>=n||y>=m) return 0;
if(visited[x][y] || visited2[x][y]) return 0;
visited2[x][y] = true;
int sum = 1;
for(auto& dir : dirs){
int nextX = dir[0] + x, nextY = dir[1] + y;
sum += dfs2(nextX, nextY);
}
return sum;
}
int dfs(int x, int y, int steps){
if(x<0||y<0||x>=n||y>=m) return 0;
if(visited[x][y]) return 0;
else if(steps == checkpoints[3]) return 1;
else if(steps == checkpoints[2]) {
if(x!=t[2].first || y!=t[2].second) return 0;
} else if(steps == checkpoints[1]) {
if(x!=t[1].first || y!=t[1].second) return 0;
} else if(steps == checkpoints[0]) {
if(x!=t[0].first || y!=t[0].second) return 0;
} else {
for(int i=0;i<4;i++) {
if(x==t[i].first && y==t[i].second)
return 0;
// manhanttan check if can reach on time
int dist = abs(x-t[i].first) + abs(y-t[i].second);
int turnLeft = checkpoints[i] - steps;
if(turnLeft > 0 && turnLeft < dist) return 0;
}
}
// do another dfs from last node to check if we can reach all nodes
visited2.assign(n,vector<bool>(m));
visited2[x][y] = true;
int cnt = dfs2(0,1);
if(cnt != total-steps) {
return 0;
}
// go all directions
visited[x][y] = true;
int path = 0;
for(auto& dir : dirs){
int nextX = dir[0] + x, nextY = dir[1] + y;
path += dfs(nextX, nextY, steps+1);
}
visited[x][y] = false;
return path;
}
int main() {
while(scanf("%d %d",&n,&m),(n+m)){
t.assign(3, {0,0});
scanf("%d %d %d %d %d %d", &t[0].first, &t[0].second,
&t[1].first, &t[1].second, &t[2].first, &t[2].second);
t.push_back(make_pair(0,1));
visited.assign(n,vector<bool>(m,false));
total = n*m;
checkpoints = {total/4,total/2,3*total/4,n*m};
printf("Case %d: %d\n",tc++,dfs(0,0,1));
}
}