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;
    }

  • 相关阅读:
    Andrew Ng机器学习算法入门((七):特征选择和多项式回归
    Andrew Ng机器学习算法入门((六):多变量线性回归方程求解
    Andrew Ng机器学习算法入门((五):矩阵和向量
    Linux常用命令集锦
    Andrew Ng机器学习算法入门(四):阶梯下降算法
    AZscaaner源码解读之数据库连接(一)
    Andrew Ng机器学习算法入门(三):线性回归算法
    Andrew Ng机器学习算法入门(二):机器学习分类
    MapServer
    fetch.js
  • 原文地址:https://www.cnblogs.com/yangyh/p/2032674.html
Copyright © 2011-2022 走看看