题目链接:http://poj.org/problem?id=2965

分析:1.这道题和之前做的poj1753题目差不多,常规思路也差不多,但是除了要输出最少步数外,还要输出路径。做这道题的时候在怎么输出bfs的路径上卡了下,然后为了方便输出试用dfs写了下,结果TLE了。T^T(不开森....

2.还有个地方调bug调了挺久的,bfs里面记录路径的时候要记录当前状态的上一个状态,结果没有判断这个状态是否加入队列过就直接改变了,见代码注释处。

3.重点是会写bfs记录路径了。

4.听说这道题有大牛的写法,去瞅了瞅,果然牛掰啊。高手思路:

> 证明:要使一个为'+'的符号变为'-',必须其相应的行和列的操作数为奇数;可以证明,如果'+'位置对应的行和列上每一个位置都进行一次操作,则整个图只有这一'+'位置的符号改变,其余都不会改变.
> 设置一个4*4的整型数组,初值为零,用于记录每个点的操作数,那么在每个'+'上的行和列的的位置都加1,得到结果模2(因为一个点进行偶数次操作的效果和没进行操作一样,这就是楼上说的取反的原理),然后计算整型数组中一的
> 个数即为操作数,一的位置为要操作的位置(其他原来操作数为偶数的因为操作并不发生效果,因此不进行操作)
*********************************
此上证其可以按以上步骤使数组中值都为‘-’
********************************
在上述证明中将所有的行和列的位置都加1后,在将其模2之前,对给定的数组状态,将所有的位置操作其所存的操作数个次数,举例,如果a[i][j]==n,则对(i,j)操作n次,当所有的操作完后,即全为‘-’的数组。
其实就是不模2的操作,作了许多的无用功。
以上的操作次序对结果无影响,如果存在一个最小的步骤,则此步骤一定在以上操作之中。(简单说下:因为以上操作已经包含了所有可改变欲改变位置的操作了)
而模2后的操作是去掉了所有无用功之后的操作,此操作同样包含最小步骤。
但模2后的操作去掉任何一个或几个步骤后,都不可能再得到全为‘-’的。(此同样可证明:因为操作次序无影响,先进行最小步骤,得到全为‘-’,如果还剩下m步,则在全为‘-’的数组状态下进行这m步操作后还得到一个全为
‘-’的数组状态,此只能是在同一个位置进行偶数次操作,与前文模2后矛盾,所以m=0),因此模2后的操作即为最小步骤的操作。

5.最近总是做位运算的题,发现异或运算真的好强大啊。

贴自己的代码:(bfs+位运算)

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
char mp[][];
struct NODE
{
int state,step;
};
NODE no;
int way[]={};
int father[]={};
int is[];
int chang[]={,,,,,,,,,,,,,,,};
queue<NODE>q;
void bfs(NODE cur)
{
q.push(cur);
is[cur.state]=;
NODE next; while(!q.empty())
{
cur=q.front();
q.pop();
if(cur.state==)
{
cout<<cur.step<<endl;
return;
}
for(int i=;i<;i++)
{
next.state=cur.state^chang[i];
//next.state=getState(cur.state,i);
next.step=cur.step+;
if(is[next.state])//要先判断状态是否出现过,再决定是否改变father[]的值
continue;
father[next.state]=cur.state;
way[next.state]=i;
if(next.state==) //注意要判断
{
cout<<next.step<<endl;
for(int j=next.state;j!=no.state;j=father[j])
cout<<(way[j]/+)<<" "<<(way[j]%+)<<endl;
return;
}
q.push(next);
is[next.state]=;
}
}
return;
}
int main()
{
no.state=;
no.step=;
for(int i=;i<;i++)
{
cin>>mp[i];
for(int j=;j<;j++)
{
if(mp[i][j]=='-')
no.state+=pow(,-*i-j);
}
}
bfs(no);
return ; }

poj2965 The Pilots Brothers' refrigerator的更多相关文章

  1. POJ2965——The Pilots Brothers' refrigerator

    The Pilots Brothers' refrigerator Description The game “The Pilots Brothers: following the stripy el ...

  2. [POJ2965]The Pilots Brothers' refrigerator (搜索/位运算)

    题意 游戏“The Pilots Brothers:跟随有条纹的大象”有一个玩家需要打开冰箱的任务. 冰箱门上有16个把手.每个手柄可以处于以下两种状态之一:打开或关闭.只有当所有把手都打开时,冰箱才 ...

  3. poj2965 The Pilots Brothers' refrigerator —— 技巧性

    题目链接:http://poj.org/problem?id=2965 题解:自己想到的方法是枚举搜索,结果用bfs和dfs写都超时了.网上拿别人的代码试一下只是刚好不超时的,如果自己的代码在某些方面 ...

  4. POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16868 ...

  5. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  6. POJ2965The Pilots Brothers' refrigerator(枚举+DFS)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22057 ...

  7. The Pilots Brothers' refrigerator(dfs)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19718 ...

  8. 枚举 POJ 2965 The Pilots Brothers' refrigerator

    题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...

  9. The Pilots Brothers' refrigerator 分类: POJ 2015-06-15 19:34 12人阅读 评论(0) 收藏

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20304 ...

随机推荐

  1. C语言 06 指针

    int *p; //第一个*是象征意义. p = &a; 等价于 int *p = &a; //第二个*是不正确的 *p = &a; //第三个*是访问指针变量指向的存储空间. ...

  2. 怎么计算一个具体InnoDB的索引大小

    一般情况下,我们看表信息可以用这个命令show table status: mysql> show table status like 't'\G . row ***************** ...

  3. wex5 实战 框架拓展之2 事件派发与data刷新

    一 前言 讲完公共data,相信大家对框架级的data组件级绑定有了更新的认识,接下来我们继续深入,以求研究明白wex5的框架能力. 在一个web项目中,其实有一个data, 是基础框架必须的data ...

  4. libtool: line 990: g++: command not found的解决

    yum -y install gcc+ gcc-c++

  5. poj3069 Saruman's Army

    http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...

  6. [LeetCode_5] Longest Palindromic Substring

    LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...

  7. django之分页、cookie装饰器

    一.分页代码如下 from django.utils.safestring import mark_safe class Page: def __init__(self, current_page, ...

  8. selenium-JS点击(项目应用)

    public static JavascriptExecutor jse; 声明一个js public LogoutWebElements(WebDriver driver){        Logo ...

  9. java 垃圾回收

    转自:http://www.360doc.com/content/13/0305/10/15643_269388816.shtml

  10. ip封包

    I P封包 從一直以來討論至今﹐我們都不斷地接觸到“封包”這個詞﹐相信您也很有興趣想知道這個“封包”究竟是個什麼樣的東東吧﹗下面就讓我們一起看看一個IP封包究竟包含了那些內容. 擷取IP封包 如果您的 ...