poj2632模拟法

来源:互联网 发布:手机淘宝怎样看到退货 编辑:程序博客网 时间:2024/06/07 06:50

又是一次ac哈哈哈,虽然题目本身不难,但是看上去很烦的样子。

不知为何我有一种高中做综合题的感觉。

但是这道题有一个陷阱,就是在输入的时候,程序crash之后应该跳过所有剩下的input执行下一个case,这里需要人工检验。

感谢Xcode的断点查错,让我明白了程序里面正在发生什么。

vector可以用自己定义的class type这点十分便利,说实话我也是第一次用。

另外要巧用bool返回的函数,如果状态比较多的话也可以考虑int返回。

#include <iostream>#include <vector>using namespace std;struct robot{    int x;    int y;    char direction;    robot(int X,int Y,char Direction){        x=X; y=Y; direction=Direction;    }};vector<robot> v;int length,side;bool test_show(int no_robot){    //check if crashes the wall    if(v[no_robot-1].x>length||v[no_robot-1].y>side||v[no_robot-1].x<=0||v[no_robot-1].y<=0){        cout<<"Robot "<<no_robot<<" crashes into the wall"<<endl;        return false;    }    int x=v[no_robot-1].x;    int y=v[no_robot-1].y;    for(int i=0;i<v.size();i++){        if(i!=no_robot-1)            if(x==v[i].x&&y==v[i].y){                cout<<"Robot "<<no_robot<<" crashes into robot "<<i+1<<endl;                return false;            }    }    return true;}bool operation(int no_robot, char instruct, int repeat){    switch (instruct) {        case 'F':            for(int i=0;i<repeat;i++){                char d=v[no_robot-1].direction;                if(d=='E'){                   v[no_robot-1].x++;                }                else if(d=='W'){                    v[no_robot-1].x--;                }                else if(d=='N'){                    v[no_robot-1].y++;                }                else if(d=='S'){                    v[no_robot-1].y--;                }                if(!test_show(no_robot))                    return false;            }            return true;            break;        case 'L':            for(int i=0;i<repeat;i++){                char d=v[no_robot-1].direction;                if(d=='E') v[no_robot-1].direction='N';                else if(d=='W') v[no_robot-1].direction='S';                else if(d=='N') v[no_robot-1].direction='W';                else if(d=='S') v[no_robot-1].direction='E';                            }            return true;            break;        case 'R':            for(int i=0;i<repeat;i++){                char d=v[no_robot-1].direction;                if(d=='E') v[no_robot-1].direction='S';                else if(d=='W') v[no_robot-1].direction='N';                else if(d=='N') v[no_robot-1].direction='E';                else if(d=='S') v[no_robot-1].direction='W';                            }            return true;            break;        default:            return true;            break;    }}bool receive_instructions(int instructions){    int j=0;    bool r=true;    for(;j<instructions;j++){        int no_robot,repeat;        char instruct;        cin>>no_robot>>instruct>>repeat;        if(r)            r=operation(no_robot, instruct, repeat);            }    return r;    }int main(int argc, const char * argv[]) {    // insert code here...    int num_case;    cin>>num_case;    for(int i=0;i<num_case;i++){        if(!v.empty())            v.clear();        cin>>length>>side;        int num_robot,instructions;        cin>>num_robot>>instructions;        for(int j=0;j<num_robot;j++){            int x,y;            char direction;            cin>>x>>y>>direction;            robot r(x,y,direction);            v.push_back(r);        }        if(receive_instructions(instructions)){            cout<<"OK"<<endl;        }                            }    return 0;}


0 0
原创粉丝点击