May the force be with you!
posts - 52,  comments - 33,  trackbacks - 0
一次AC了这个模拟题,本来是想用面向对象的思想来写,不过最后写成了这个样子。。。
                            simbaforrest
                            2007/12/27
#include <iostream>
#include 
<stdlib.h>
#include 
<string.h>
using namespace std;

class map
{
    
public:
        
int WE;
        
int NS;
        
int grid[110][110];
        map() {};
        
void init(int we, int ns)
        {
            WE 
= we;
            NS 
= ns;
            memset(grid,
0,sizeof(grid));
        };
        inline 
void setR(int we, int ns, int rid)
        {grid[ns][we]
=rid;}
        
void show()
        {
            printf(
"map:WE=%d,NS=%d\n",WE,NS);
            
for(int i=NS; i>0; i--)
            {
                
for(int j=1; j<=WE; j++)
                    printf(
"%d",grid[i][j]);
                printf(
"\n");
            }
        }
        
void report(int rbt=-1int crash=-8)
        {
            
if(rbt == -1)
                printf(
"OK\n");
            
else if(crash == -1)
                printf(
"Robot %d crashes into the wall\n",rbt);
            
else
                printf(
"Robot %d crashes into robot %d\n",rbt,crash);
        };
//        friend class robot;
};
map Map;

class robot
{
    
public:
        
//static int totalnum;
        int id;
        
int ns,we;
        
int face;
        
int crash;
        robot() {};
//        friend class command;
        void init(int rid,int WE, int NS, char F)
        {
            id 
= rid;
            ns 
= NS;
            we 
= WE;
            Map.setR(we,ns,id);
            crash 
= 0;
            
switch(F)
            {
                
case 'N':face = 0;return;
                
case 'S':face = 2;return;
                
case 'W':face = 3;return;
                
case 'E':face = 1;return;
            }
        };
        
        inline 
void turnL(){face = ((face-1)+4)%4;};
        inline 
void turnR(){face = (face+1)%4;};
        
void Move()
        {
            Map.grid[ns][we] 
= 0;
            
switch(face)
            {
                
case 0:
                    ns
++;break;
                
case 1:
                    we
++;break;
                
case 2:
                    ns
--;break;
                
case 3:
                    we
--;break;
            }
            iscrash();
            
if(crash==0)
                Map.grid[ns][we] 
= id;
        };
        inline 
void iscrash()
        {
            
if(ns<=0 || ns>Map.NS || we<=0 || we>Map.WE)
            {
                crash 
= -1;//crash the wall
                return;
            }
            
int idtmp = Map.grid[ns][we];
            
if( idtmp!=0 )
            {
                crash 
= idtmp;//crash the idtmp robot
            }
        };
};
const int maxn = 101;
robot R[
101];

class command
{
    
public:
        
//static int totalnum;
        int robotid;
        
char action;
        
int repeat;
        command() {};
        
void init(int id, char act, int rep)
        {
            robotid 
= id;
            action 
= act;
            repeat 
= rep;
        };
        bool doaction()
        {
            
switch(action)
            {
                
case 'L':
                    
while(repeat--)
                    {R[robotid].turnL();}
return 1;
                
case 'R':
                    
while(repeat--)
                    {R[robotid].turnR();}
return 1;
                
case 'F':
                    
while(repeat--)
                    {
                        R[robotid].Move();
                        
int tmp = R[robotid].crash;
                        
if(tmp!=0)
                        {
                            
//Map.show();
                            Map.report(robotid,tmp);
//                            printf("(%d,%d)\n",R[robotid].ns,R[robotid].we);
                            return 0;
                        }
                    }
                    
return 1;
            }
        };
};
command C[
101];
int robotnum,commandnum;

void init()
{
    
int A,B;
    scanf(
"%d%d",&A,&B);
    Map.init(A,B);
//    Map.show();
    int n,m;
    scanf(
"%d%d",&n,&m);
    robotnum 
= n;
    commandnum 
= m;
    
for(int i=1; i<=n; i++)
    {
        
int we,ns;
        
char F[5];
        scanf(
"%d%d%s",&we,&ns,F);
        R[i].init(i,we,ns,F[
0]);
    }
    
for(int i=1; i<=m; i++)
    {
        
int rid,rep;
        
char act[5];
        scanf(
"%d%s%d",&rid,act,&rep);
        C[i].init(rid,act[
0],rep);
    }
//    Map.show();
}

void begin()
{
    
int m = commandnum;
    
for(int i=1; i<=m; i++)
    {
        
//cout<<i<<":\n";
        if(!C[i].doaction())
            
return;
        
//Map.show();
    }
    Map.report();
}

int main()
{
    
int K;
    scanf(
"%d",&K);
    
while(K--)
    {
        init();
        begin();
    }
    
return 0;
}



嗯,下面就是我的代码了,很久以前写的了。
思想比较简单,就不写了,不过当时wa了一次:
                                                 ————by littlekid
  1 Source Code
  2 Problem: 2632        User: LittleKid
  3 Memory: 216K        Time: 0MS
  4 Language: G++        Result: Accepted
  5 
  6 
  7 # include <stdio.h>
  8 # include <string.h>
  9 
 10 # define N 111
 11 
 12 typedef struct _robot{
 13     int x,y;
 14     int dir;
 15 };
 16 
 17 int a,b;
 18 int m,n;
 19 _robot robot[N];
 20 int map[N][N];
 21 bool OK;
 22 
 23 void initialize(){
 24     for (int i = 0; i < N; i ++){
 25         map[i][0= 0;
 26         map[0][i] = 0;
 27     }
 28 }
 29 
 30 void init(){
 31     scanf("%d %d",&a,&b);
 32     for (int i = 1; i <= a; i ++){
 33         for (int j = 1; j <= b; j ++){
 34             map[i][j] = -1;
 35         }
 36         map[i][b+1= 0;
 37     }
 38     for (int i = 0; i <= b; i ++){
 39         map[a+1][i] = 0;
 40     }
 41 
 42     scanf("%d %d",&n,&m);
 43     int x,y;
 44     char d;
 45     for (int i = 1; i <= n; i ++){
 46         scanf("%d %d %c",&x,&y,&d);
 47         if (map[x][y] == 0) printf("Eroor\n");
 48         map[x][y] = i;
 49         robot[i].x = x; robot[i].y = y;
 50         switch (d){
 51             case 'N':robot[i].dir = 3;
 52                 break;
 53             case 'E':robot[i].dir = 2;
 54                 break;
 55             case 'S':robot[i].dir = 1;
 56                 break;
 57             case 'W':robot[i].dir = 0;
 58                 break;
 59             default: printf("ERROR\n");
 60         }
 61     }
 62     OK = true;
 63 }
 64 
 65 void L(int no, int re){
 66     robot[no].dir = (robot[no].dir + re) % 4;
 67     robot[no].dir += 4;
 68     robot[no].dir %= 4;
 69 }
 70 
 71 void isOK(int no){
 72   //  printf("  %d %d %d %d\n",no,robot[no].x,robot[no].y,robot[no].dir);
 73     int x = robot[no].x;
 74     int y = robot[no].y;
 75     if (map[x][y] >= 0){
 76         OK = false;
 77         if (map[x][y] == 0){
 78             printf("Robot %d crashes into the wall\n",no);
 79         }
 80         else {
 81             printf("Robot %d crashes into robot %d\n",no,map[x][y]);
 82         }
 83         return ;
 84     }
 85     map[x][y] = no;
 86 }
 87 
 88 void Forward(int no){
 89     map[robot[no].x][robot[no].y] = -1;
 90 
 91     switch (robot[no].dir){
 92         case 0: robot[no].x -= 1;
 93             break;
 94         case 1: robot[no].y -= 1;
 95             break;
 96         case 2: robot[no].x += 1;
 97             break;
 98         case 3: robot[no].y += 1;
 99             break;
100         }
101     isOK(no);
102 }
103 
104 void F(int no, int re){
105     while (OK && re--){
106         Forward(no);
107     }
108 }
109 
110 void simulation(){
111     int no,re;
112     char action;
113     for (int i = 0; i < m; i ++){
114         scanf("%d %c %d",&no,&action,&re);
115         if (OK){
116             switch (action){
117                 case 'L': L(no,re);
118                 break;
119                 case 'R': L(no,-re);
120                 break;
121                 case 'F': F(no,re);
122                 break;
123                 default: printf("ERROR!\n");
124             }
125         }
126     }
127     if (OK) printf("OK\n");
128 }
129 
130 int main(){
131     int T;
132     initialize();
133     scanf("%d",&T);
134     while (T--){
135         init();
136         simulation();
137     }
138     return 0;
139 }




posted on 2007-12-27 15:21 R2 阅读(862) 评论(0)  编辑 收藏 引用 所属分类: Problem Solving

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


你是第 free hit counter 位访客




<2007年12月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(4)

随笔分类(54)

随笔档案(52)

文章档案(1)

ACM/ICPC

技术综合

最新随笔

搜索

  •  

积分与排名

  • 积分 - 61681
  • 排名 - 357

最新评论

阅读排行榜

评论排行榜