您的当前位置:首页正文

猫捉老鼠

2023-11-21 来源:步旅网
è׽ÀÏÊóһֻèºÍÒ»Ö»ÀÏÊóÔÚ10*10µÄÃÔ¹¬ÖС£ÃÔ¹¬ÖеÄÿ¸ö·½¸ñ¿ÉÒÔÊǿյģ¬»òÕߺ¬ÓÐÕÏ°-¡£Ã¨ºÍÀÏÊó¿ÉÒÔ½øÈëÈÎÒâÒ»¸ö¿ÕµÄ·½¸ñÖС£µ±ËûÃÇÏàÓöʱ£¬Ã¨ºÍÀÏÊóÔÚͬһ¸ö·½¸ñÖС£ µ«ÊÇ£¬ÎÞÂÛè»òÀÏÊ󶼲»ÄܽøÈëÓÐÕÏ°-µÄ·½¸ñ¡£ÎÒÃÇ¿ÉÒÔÓÃ×Ö·û×é³ÉµÄ¶þάÊý×é±íʾÃÔ¹¬£¬ÈçÏÂͼËùʾ¡£

ÀÏÊóÔÚÃÔ¹¬Öа´ÕÕÒ»Ö̶ֹ¨µÄ·½Ê½ÐÐ×ߣºÃ¿¸öʱ¿Ì£¬ÀÏÊó¶¼ÏòËüËùÃæ¶ÔµÄ·½ÏòÇ°½øÒ»¸ñ£¬ÕâÐèÒª»¨·Ñ£±Ãëʱ¼ä¡£Èç¹ûÇ°·½ÊÇÒ»¸öÕÏ°-»òÕßÊÇÃÔ¹¬µÄ±ß½ç£¬Ëü½«»¨£±ÃëµÄʱ¼ä °´Ë³Ê±Õë·½Ïòת90¶È¡£

ΪÁË×¥µ½ÀÏÊó£¬Õâֻè¾ö¶¨Ò²°´ÕÕÓëÀÏÊóÏàͬµÄÐÐ×ß·½Ê½Ðнø¡£

èºÍÀÏÊóÔÚÿ¸öµ¥Î»Ê±¼äÄÚÊÇͬʱÐж¯µÄ¡£Òò´Ë£¬Èç¹ûèºÍÀÏÊóÔÚÐнø¹ý³ÌÖС°²Á¼ç¶ø¹ý¡±£¬Ã¨ÊÇÎÞ·¨×½µ½ÀÏÊóµÄ¡£Ö»Óе±Ã¨ºÍÀÏÊóͬʱµ½´ïÒ»¸öÏàͬµÄ¸ñ×Óʱ£¬ è²ÅÄÜ׽סÀÏÊó¡£

³õʼʱ£¬Ã¨ºÍÀÏÊó²»»áÔÚͬһ¸ö·½¸ñÖС£²¢ÇÒËüÃǶ¼ÃæÏò±±·½¡£ ÄãµÄÈÎÎñÊDZàÒ»¸ö³ÌÐò£¬Çó³öè׽µ½ÀÏÊóµÄËù»¨Ê±¼ä¡£

ÊäÈëÊä³ö¸ñʽ

ÊäÈëÊý¾ÝµÄµÚÒ»ÐÐn£¬±íʾÊäÈëÊý¾ÝµÄ×éÊý¡£

ÿ×éÊý¾ÝÓÉ10ÐÐ×é³É£¬Ã¿ÐÐ10¸ö×Ö·û£¬±íʾÃÔ¹¬µÄµØͼÒÔ¼°Ã¨ºÍÀÏÊóµÄ³õʼλÖá£ÊäÈëÊý¾Ý±£Ö¤Ö»ÓÐһֻèºÍÒ»Ö»ÀÏÊó¡£

ÿ×éÊäÈëÊý¾ÝÖ®ºó¾ùÓÐÒ»¸ö¿ÕÐÐ×÷Ϊ¼ä¸ô¡£

¶ÔÓÚÿ×é¸ø¶¨µÄÊäÈ룬Êä³öÒ»Ðнöº¬Ò»¸öÊý£¬¼´Ã¨×½µ½ÀÏÊóËù»¨µÄʱ¼ä¡£Èç¹ûèÓÀÔ¶¶¼ÎÞ·¨×¥µ½ÀÏÊó£¬ÔòÊä³ö0¡£

ÑùÀýÊäÈë 1

*...*..... ......*... ...*...*.. .......... ...*.c.... *.....*... ...*...... ..m......* ...*.*.... .*.*......

ÑùÀýÊä³ö

49

À´Ô´£ºhttp://acm.fzu.edu.cn/problem.php?pid=1019

============================ #include #include void main() {

int zhuiji(char e[][11],int a,int b);

int zhuiji1(char e[][11],int a,int b,int m,int n); char e[10][11]; int a,b,i;

printf(\"Çë³öÈëÃÔ¹¬Í¼ÐÎ\\n\"); for(i=0;i<10;i++) {

gets(e[i]); }

printf(\"Çë³öÈëÀÏÊóλÖÃ:\\n\"); scanf(\"%d%d\

printf(\"%d\\n\ system(\"pause\"); }

int zhuiji1(char e[][11],int a,int b,int t,int m,int n) {

int num1=1,p=1,q=0; while(p<=t+1) {

p++; if(q==t) {

if(a==m&&b==n) {

return 1; break; } else {

return 0;

} }

if(num1==1) {

if(e[m-1][n]!='/'&&m!=0) {

q++; m=m-1; continue; } else {

q++; num1=2; continue; }

}

else if(num1==2) {

if(e[m][n+1]!='/'&&n!=9) {

q++; n=n+1; continue; } else {

q++; num1=3; continue; }

}

else if(num1==3) {

if(e[m+1][n]!='/'&&m!=9) {

q++; m=m+1; continue; }

else {

q++; num1=4; continue; } }

else if(num1==4) {

if(e[m][n-1]!='/'&&n!=0) {

q++; n=n-1; continue; } else {

q++; num1=1; continue; } } } p=1; }

int zhuiji(char e[][11],int a,int b) {

int t=0,num=1,k=100,m,n;

printf(\"ÇëÊäÈëèµÄλÖãº\\n\"); scanf(\"%d%d\ while(k>0) {

k--;

if(zhuiji1(e,a,b,t,m,n)) {

return t; break; }

if(num==1) {

if(e[a-1][b]!='/'&&a!=0)

{

t++; a=a-1; continue; } else {

t++; num=2; continue; }

}

else if(num==2) {

if(e[a][b+1]!='/'&&b!=9) {

t++; b=b+1; continue; } else {

t++; num=3; continue; }

}

else if(num==3) {

if(e[a+1][b]!='/'&&a!=9) {

t++; a=a+1; continue; } else {

t++; num=4; continue;

} }

else if(num==4) {

if(e[a][b-1]!='/'&&b!=0) {

t++; b=b-1; continue; } else {

t++; num=1; continue; } } } }

------------------------------ #include using namespace std;

struct location {

int x; int y; };

class animal {

public:

animal( location loc );

static void getMiGong( char p[10][10] ); location getLocation(); void walking(); private:

static char (*pMiGong)[10];//Ö¸ÏòÃÔ¹¬Êý×é location loc; int drection;//Éϱ±ÏÂÄÏ×óÎ÷ÓÒ¶«´Ó0¿ªÊ¼,±ä»»·½Ïòdrection×Ô¼Ó,¸ù¾ÝÓàÊýÅжϷ½Ïò. void northWalking();//Ïò±±×ß

void eastWalking();//Ïò¶«×ß void southWalking();//ÏòÄÏ×ß void westWalking();//ÏòÎ÷×ß };

char (*animal::pMiGong)[10]={0};//ÀàÍâ¼ÓÕâÒ»¾äÊÔÊÔ

animal::animal( location loc ) {

this->loc = loc; this->drection = 0; }

void animal::getMiGong( char p[][10] ) {

pMiGong = p; }

location animal::getLocation() {

return this->loc; }

void animal::walking() {

int drectmp = this->drection % 3)×¢ÒâÊÇ˳ʱÕë0±±1¶«2ÄÏ3Î÷ switch( drectmp ) {

case 0:

this->northWalking(); return; case 1:

this->eastWalking(); return; case 2:

this->southWalking(); return; case 3:

this->westWalking(); return; default:

cout << \"Drection Error!!!!\" << endl; return;

4;//È¡µ±Ç°µÄʵ¼Ê·½ÏòÊýÖµ(0 -

} }

void animal::northWalking() // drection % 4 == 0 {

if( this->loc.x != 0 && pMiGong[ this->loc.x - 1 ][ this->loc.y ] == '.' )//¼ì²éÊÇ·ñ¿É×ß {

-- this->loc.x;//ÔòÏò±±×ß } else {

++this->drection;//²»ÄÜ×ßÔòתÏò } }

void animal::eastWalking() // drection % 4 == 1 {

if( this->loc.y != 9 && pMiGong[ this->loc.x '.' )//¼ì²éÊÇ·ñ¿É×ß {

++ this->loc.y; //Ïò¶«×ß } else {

++this->drection;//²»ÄÜ×ßÔòתÏò } }

void animal::southWalking()// drection % 4 == 2 {

if( this->loc.x != 9 && pMiGong[ this->loc.x '.' )//¼ì²éÊÇ·ñ¿É×ß {

++ this->loc.x;//ÏòÄÏ×ß } else {

++this->drection;//²»ÄÜ×ßÔòתÏò } }

void animal::westWalking()//drection % 4 == 3 {

][ this->loc.y + 1 + 1 ][ this->loc.y ] == ] == if( this->loc.y != 0 && pMiGong[ this->loc.x ][ this->loc.y - 1 ] == '.' )//¼ì²éÊÇ·ñ¿É×ß {

-- this->loc.y;//ÔòÏòÎ÷×ß } else {

++this->drection;//²»ÄÜ×ßÔòתÏò } }

int main() {

char MiGong[10][10]; int i(0), j(0);

location locCat, locMouse;

while( i < 10 ) {

while( j < 10 ) {

cin >> MiGong[i][j];

if( MiGong[i][j] == 'c' ) {

locCat.x = i; locCat.y = j; }

else if( MiGong[i][j] == 'm' ) {

locMouse.x = i; locMouse.y = j; }

++ j; }

j = 0; ++i; }

animal::getMiGong( MiGong );

animal Cat( locCat ), Mouse( locMouse ); int n(0),control(0);

while( Cat.getLocation().x != Mouse.getLocation().x || Cat.getLocation().y != Mouse.getLocation().y ) {

//cout << \"Cat(\" << Cat.getLocation().x << \Mouse(\" << Mouse.getLocation().x << \

<< Mouse.getLocation().y << \")\" << endl; if( control == 0 ) {

control = 1; Cat.walking(); ++ n; } else {

control = 0;

Mouse.walking(); } }

cout << n << endl; system( \"pause\" ); return 0; }

因篇幅问题不能全部显示,请点此查看更多更全内容