## Algorithm

Inspired by the ice sculptures in Harbin, the members of the programming team from Arctic University of Robotics and Automata have decided to hold their own ice festival when they return home from the contest. They plan to harvest blocks of ice from a nearby lake when it freezes during the winter. To make it easier to monitor the thickness of the ice, they will lay out a rectangular grid over the surface of the lake and have a lightweight robot travel from square to square to measure ice thickness at each square in the grid. Three locations in the grid are specified as “check-in” points and the robot is supposed to radio a progress report from these points when it is one-fourth, one-half, and three-fourths of the way through its tour of inspection. To avoid unnecessary wear and tear on the surface of the ice, the robot must begin its tour of the grid from the lower left corner, designated in (row,column) coordinates as (0,0), visiting every other grid location exactly once and ending its tour in row 0, column 1. Moreover, if there are multiple tours that the robot can follow, then a different one is to be used each day. The robot is able to move only one square per time step in one of the four compass directions: north, south, east, or west. You are to design a program that determines how many different tours are possible for a given grid size and a sequence of three check-in points. For example, suppose the lake surface is marked off in a 3 × 6 grid and that the check-in points, in order of visitation, are (2,1), (2,4), and (0,4). Then the robot must start at (0,0) and end at (0,1) after visiting all 18 squares. It must visit location (2,1) on step 4 (= ⌊18/4⌋), location (2,4) on step 9 (= ⌊18/2⌋), and location (0,4) on step 13 (= ⌊3 × 18/4⌋). There are just two ways to do this (see Figure 8). Note that when the size of the grid is not divisible by 4, truncated division is used to determine the three check-in times. Note that some configurations may not permit any valid tours at all. For example, in a 4 × 3 grid with check-in sequence (2,0), (3,2), and (0,2), there is no tour of the grid that begins at (0,0) and ends at (0,1). Input The input contains several test cases. Each test case begins with a line containing two integers m and n, where 2 ≤ m, n ≤ 8, specifying the number of rows and columns, respectively, in the grid. This is followed by a line containing six integer values r1, c1, r2, c2, and r3, c3, where 0 ≤ ri < m and 0 ≤ ci < n for i = 1, 2, 3. Following the last test case is a line containing two zeros. Output Display the case number, beginning at 1, followed by the number of possible tours that begin at row 0, column 0, end at row 0, column 1, and visit row ri , column ci at time ⌊i × m × n/4⌋ for i = 1, 2, 3. Follow the format of the sample output.

Sample Input 3 6 2 1 2 4 0 4 4 3 2 0 3 2 0 2 0 0

Sample Output Case 1: 2 Case 2: 0

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````#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));
}
}
```
```
Copy The Code &

Input

cmd
3 6
2 1 2 4 0 4
4 3
2 0 3 2 0 2
0 0

Output

cmd
Case 1: 2
Case 2: 0