A training-cat and mouse-simulation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.