zoukankan      html  css  js  c++  java
  • POJ2965(TLE,Why?)

    #include <stdafx.h>
    #include <iostream>
    #define MAX_STATE 65535
    #define ALL_BLACK 65535
    #define ALL_WHITE 0
    #define WIDTH 4
    #define HEIGTH 4
    #define SIZE_OF_BOARD WIDTH*HEIGTH
    #include <queue>

    using namespace std;

    int flip(int current,int pos){
        int row = (pos/WIDTH)*WIDTH;
        int b = current;
        current ^=1<<(row );
        current ^=1<<(row + 1);
        current ^=1<<(row + 2);
        current ^=1<<(row + 3);
        int col = pos%4;
        current ^=1<<(col);
        current ^=1<<(col+4);
        current ^=1<<(col+8);
        current ^=1<<(col+12);
        current ^=1<<pos;
        return current;
    }

    int main(int argc, char* argv[])
    {
        //freopen("d:/t.txt","r",stdin);
        int step[MAX_STATE];
        int pre[MAX_STATE];
        int pos[MAX_STATE];
        int current_state = 0,next_state;
        char c;
        for(int i=0;i<SIZE_OF_BOARD;i++)
        {
            cin>>c;
            current_state += (c=='+')<<(i);
        }
        if(current_state==ALL_WHITE){
            printf("0\n");
            return 0;
        }
        memset(step,-1,sizeof(step));
        queue<int> q;
        q.push(current_state);
        step[current_state]=0;
        pre[current_state]=-1;
        while(!q.empty()){
            current_state = q.front();
            q.pop();
            for(int i=0;i<SIZE_OF_BOARD;i++)
            {
                next_state = flip(current_state,i);
                if(next_state==ALL_WHITE){
                    pre[next_state] = current_state;
                    pos[next_state] = i;
                    int count = step[current_state]+1;
                    int ans[16];
                    printf("%d\n",count);
                    for(int c = count-2;c>=0;c--)
                    {
                        ans[c]=pos[pre[next_state]];
                        next_state = pre[next_state];
                    }
                    ans[count-1]=i;
                    for(int i=0;i<count;i++)
                        printf("%d %d\n",ans[i]/4+1,ans[i]%4+1);
                    return 0;
                }
                if(-1==step[next_state]){
                    pre[next_state] = current_state;
                    pos[next_state] = i;
                    step[next_state] = step[current_state]+1;
                    q.push(next_state);
                }
            }
        }
        printf("Impossible\n");
        return 0;
    }

  • 相关阅读:
    POJ 2230 Watchcow 欧拉回路的DFS解法(模板题)
    POJ 2188 Cow Laundry
    ACM一年记,总结报告(希望自己可以走得很远)
    [USACO1.5]回文质数 Prime Palindromes
    POJ-1276-Cash Machine(多重背包)
    POJ-1322 Chocolate(概率DP)
    POJ-1179 Polygon (动态规划)
    HDU-1520 Anniversary party(树形DP)
    HDU-1054 Strategic Game(树形DP)
    POJ-1157 LITTLE SHOP OF FLOWERS(动态规划)
  • 原文地址:https://www.cnblogs.com/yangyh/p/2032674.html
Copyright © 2011-2022 走看看