细节,细节,细节!!---PKU 1753 --FLIP GAME

我又一次倒在细节的脚下。
当下午过来,两位同伴告诉我错误的原因,我又一次感到深深的恐惧和自责。
怎么又是细节问题???!!!
初始化的问题!
在逻辑方面,我往往没有出现过错误!
这一点,以后我应当注意。
但在把逻辑用语言表达出来的时候,老是少写一些细节方面的东西,以后,这些东西应当非常非常的十分注意!!!。
下面是PKOJ 1753源代码:

Source Code




















Problem: 1753 User: omycle
Memory: 756K Time: 16MS
Language: C++ Result: Accepted


  • Source Code
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    #define SIZE 0XFFFF
    int step[SIZE];//记录步数
    int duilie[SIZE+1];//0记录初始状态,将来的数值全部保存到这里了,并有两个指针指向头和尾部
    int tou=0; //队列头部
    int wei=0; //队列尾部
    bool flag[SIZE+1];//非顺序存储
    int b[17];//定义17个
    void Init()
    {
    memset(step,0,SIZE*sizeof(int));//记录步数
    memset(duilie,0,(SIZE+1)*sizeof(int));//记录数值是多少
    memset(flag,false,(SIZE+1)*sizeof(bool));//记录哪个状态已经OK了,不要在重复计算了
    }
    void set_bits()
    {
    //memset(b,1,17*sizeof(int));
    for(int i=1;i<=16;i++)
    {
    b[i]=1;
    b[i]<<=(i-1);
    }
    }
    void read()
    {
    //用duilie[0]来保存最初始的状态
    char s[5];
    s[4]=0;
    duilie[0]=0;
    for(int i=1;i<=4;i++)
    {
    scanf("%s",&s);
    for(int j=0;j<4;j++)
    {
    duilie[0]<<=1;
    if(s[j]=='B'||s[j]=='b')
    duilie[0]+=1;
    }
    }
    }
    //开始广度优先遍历
    void Dfs()
    {
    //将初始状态存入数组duilie[]中
    tou=0;
    wei=1;
    step[tou]=0;
    flag[duilie[tou]]=true;
    int temp=duilie[tou];
    if(temp==0||temp==0xffff)
    {
    printf("%d\n",step[wei]);
    return ;
    }
    while(wei>tou)
    {
    temp=duilie[tou];
    for(int i=1;i<=16;i++)
    {
    temp=duilie[tou];/////细节,细节,细节!!!
    temp^=b[i]; /*自身按位取反*/
    //int left,right,north,south;
    if(i==5||i==9||i==13)
    {
    //没有右
    if((i+1)<=16)
    temp^=b[i+1];
    if((i+4)<=16)
    temp^=b[i+4];
    if((i-4)>0)
    temp^=b[i-4];
    }
    else if(i%4==0)
    { //没有左
    if((i-1)>0)
    temp^=b[i-1];
    /* if((i+1)<16&&(i+1)<16)
    temp^=b[i+1];
    */
    if((i+4)<=16)
    temp^=b[i+4];
    if((i-4)>0)
    temp^=b[i-4];
    }
    else
    {
    if((i-1)>0)
    temp^=b[i-1];
    if((i+1)<=16)
    temp^=b[i+1];
    if((i+4)<=16)
    temp^=b[i+4];
    if((i-4)>0)
    temp^=b[i-4];
    }
    //
    if(!flag[temp])
    {
    flag[temp]=true;
    duilie[wei]=temp;
    step[wei]=step[tou]+1;
    if(temp==0||temp==0xffff)
    {
    printf("%d\n",step[wei]);
    return ;
    }

    wei++;//尾向后走
    }
    }
    tou++;//头向后走
    }
    printf("Impossible\n");
    // system("PAUSE");
    }
    int main()
    {
    Init();
    read();
    set_bits();
    Dfs();
    return 0;
    }


评论

此博客中的热门博文

Linux/ARM Page Table Entry 属性设置分析

由RFE指令引发的一串故事

提交了30次才AC ---【附】POJ 2488解题报告