The cat and the mouse
"Title description"
Cats and mice move in 10*10 squares, for example:
*...*.....
......*...
...*...*..
..........
...*. C....
*.....*...
...*......
.. m......*
...*.*....
.*.*......
C= Cat (CAT)
m= Mouse (MOUSE)
*= obstacles
. = Open Space
The cat and mouse walk one cell per second, and if they are in the same lattice at the end of a second, we call them "meet."
Note that "to wear" is not a meeting. The cat and mouse move the same way: usually along the straight line, the next step if you will walk to
Obstacles go up or out of bounds, it takes 1 seconds to do a right turn 90 degrees. At first they were all facing the north.
Programmed to calculate how many seconds later they meet.
"input Format"
10 lines, formatted as above
"Output format"
Meet time t. If there is no solution, output-1.
"Sample Input"
*...*..... ......*... ...*...*.. .......... ...*. C ..... *.....* ......... m......* .... * * * ..... *.*......
"Sample Output"
49
AC Code:
#include <cstdio> #include <string> #include <cstring> #include <iostream> #include < algorithm> #include <cmath> #include <stack> #include <queue> #include <set> #define LL Long Long#define INF 0x7fffffffusing namespace Std;char map[15][15]; int CX, CY, MX, my;//represent the position of the cat and mouse respectively int xx[4] = {-1, 0, 1, 0};//direction int yy[4] = {0, 1, 0, -1};//direction int cdir, mdir;//for cat and mouse respectively to int judge () {//To determine if the current encounters if (cx = = mx && cy = = my) return 1;return 0;} int main () {for (int i = 1; i <=; i++) {scanf ("%s", Map[i] + 1);} for (int i = 1; I <=, i++) {for (int j = 1; J <=; J + +) {if (map[i][j] = = ' C ') {cx = i; cy = j;} if (map[i][j] = = ' M ') {mx = i; my = J;}}} printf ("%d%d%d%d \ n", CX, CY, MX, my); int ans = 0;cdir = 0; Mdir = 0;while (1) {//while (1) {if (map[cx + Xx[cdir]][cy + yy[cdir]]! = ' * ' && cx + xx[cdir] <= && CX + Xx[cdir] >= 1 && cy + yy[cdir] <= && cy + yy[cdir] >= 1) {cx = cx + Xx[cdir]; CY= cy + Yy[cdir];//break;} else Cdir = (cdir + 1)% 4;//}//while (1) {if (map[mx + Xx[mdir]][my + yy[mdir]]! = ' * ' && mx + xx[mdir] <= &A mp;& mx + xx[mdir] >= 1 && my + yy[mdir] <= && my + yy[mdir] >= 1) {mx = mx + xx[mdir]; my = my + yy[mdir];//break;} else Mdir = (mdir + 1)% 4;//}ans ++;if (ans >= 100000) break; Used to determine if it is possible to meet, so big dot place 100000 should be enough, although there is contingency if (judge ()) break;} if (ans = = 100000) printf (" -1\n"), Else printf ("%d\n", ans); return 0;}
A training-cat and mouse-simulation