TJU1031

ⅰ亾dé卋堺 提交于 2019-11-27 01:45:40
直接模拟做。棋盘共有(10*10*4)^2=160000种状态,循环160000次以后还没相遇就认为无解了。
代码方面么……自己喜欢设计点class,估计看上去好理解一些哈~~
None.gif#include<iostream>
None.gif
using namespace std;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
enum Directiondot.gif{North,East,South,West};
ExpandedBlockStart.gifContractedBlock.giftypedef 
structdot.gif{int x,y;Direction d;}Point;
None.gif
class Map
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public:
InBlock.gif    Map();
InBlock.gif    
int meet();
InBlock.gif    
void move();
InBlock.gif    
int seconds;
InBlock.gif
private:
InBlock.gif    
int item[10][10];
InBlock.gif    Point Cat,Mouse;
InBlock.gif    
void Fill(char s,int x,int y);
InBlock.gif    
int movable(Point& pos);
ExpandedBlockEnd.gif}
;
None.gif
int main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
int data;
InBlock.gif    cin
>>data;
InBlock.gif    Map
* m;
InBlock.gif    
while(data-- > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        m
=new Map();
InBlock.gif        
while(!m->meet())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m
->move();
InBlock.gif            
if(m->seconds>160000l)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m
->seconds = -1;
InBlock.gif                
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        cout
<<m->seconds<<endl;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gifMap::Map()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
char s[10];
InBlock.gif    
int count1,count2;
InBlock.gif    
for(count1=0;count1<10;count1++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cin
>>s;
InBlock.gif        
for(count2=0;count2<10;count2++)
InBlock.gif            Fill(s[count2],count1,count2);
ExpandedSubBlockEnd.gif    }

InBlock.gif    seconds 
= 0;
InBlock.gif    Cat.d 
= Mouse.d = North;
ExpandedBlockEnd.gif}

None.gif
int Map::meet()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{return (Cat.x==Mouse.x && Cat.y==Mouse.y);}
None.gif
void Map::Fill(char s,int x,int y)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
switch(s)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
case '*': item[x][y]=0;break;
InBlock.gif    
case '.': item[x][y]=1;break;
InBlock.gif    
case 'C': item[x][y]=1;Cat.x=x,Cat.y=y;break;
InBlock.gif    
case 'M': item[x][y]=1;Mouse.x=x,Mouse.y=y;break;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
void Map::move()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
if(!movable(Cat)) Cat.d=(Direction)((Cat.d+1)%4);
InBlock.gif    
if(!movable(Mouse)) Mouse.d=(Direction)((Mouse.d+1)%4);
InBlock.gif    seconds
++;
ExpandedBlockEnd.gif}

None.gif
int Map::movable(Point& pos)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
switch(pos.d)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
case North:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(0!=pos.x && item[pos.x-1][pos.y])dot.gif{pos.x--;return 1;}
InBlock.gif        
break;
InBlock.gif    
case East:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(9!=pos.y && item[pos.x][pos.y+1])dot.gif{pos.y++;return 1;}
InBlock.gif        
break;
InBlock.gif    
case South:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(9!=pos.x && item[pos.x+1][pos.y])dot.gif{pos.x++;return 1;}
InBlock.gif        
break;
InBlock.gif    
case West:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(0!=pos.y && item[pos.x][pos.y-1])dot.gif{pos.y--;return 1;}
InBlock.gif        
break;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/FancyMouse/articles/243178.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!