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

题解:自己想到的方法是枚举搜索,结果用bfs和dfs写都超时了。网上拿别人的代码试一下只是刚好不超时的,如果自己的代码在某些方面不够优化,那超时很正常。看来这题用dfs和bfs都不是好办法。 然后又看到比较厉害的技巧:“可知翻偶数次等于没翻,即没有翻的必要,翻奇数次的结果与翻一次的结果一样“”。有了这个重要结论,那么可以具体操作了:设一个二维数组以记录每个的翻转次数。对于每个‘+’,都翻转其所在的行列(注意‘+’只翻一次),然后更新翻转次数。最后统计翻转次数为奇数的,即为实际需要翻转的。(为什么我没想到这种方法。需要仔细体会这种思维)

代码如下:

 #include<stdio.h>//poj2965
#include<string.h>
int main()
{
int a[][],sum;
char map[][];//map开一维已足够
memset(a,,sizeof(a));
for(int i = ; i<; i++)
{
scanf("%s",map[i]);
for(int j = ; j<; j++)
{
if(map[i][j]=='+')
{
a[i][j]--;//‘+’在下面的步骤中翻了两次,要减少一次
for(int k = ; k<; k++)
{
a[k][j]++;
a[i][k]++;
}
}
}
}
sum = ;
for(int i = ; i<; i++)
for(int j = ; j<; j++)
{
if(a[i][j]%) sum++;
}
printf("%d\n",sum); for(int i = ; i<; i++)
for(int j = ; j<; j++)
{
if(a[i][j]%)
printf("%d %d\n",i+,j+);
}
}

对了,附上自己wa了的dfs和bfs;

dfs:

 #include<cstdio>//poj 2965dfs 超时未过
#include<cstring>
#define MIN(a,b) (a<b?a:b) int a[],ans[]; int get()
{
for(int i = ; i<; i++)
if(a[i]) return ;
return ;
} void flip(int loc)
{
int x = loc/, y = loc%;
a[loc] = !a[loc];
for(int k = ; k<; k++)
a[x*+k] = !a[x*+k];
for(int k = ; k<; k++)
a[k*+y] = !a[k*+y];
} int dfs(int loc,int step,int tar)
{
if(loc>) return ; if(step==tar && get())
return ; else
{
flip(loc);
ans[step] = loc;
if(dfs(loc+,step+,tar)) return ;
flip(loc); if(dfs(loc+,step,tar)) return ; }
return ;
} int main()
{
int t;
char s[]; memset(ans,-,sizeof(ans));
for(int i = ; i<; i++)
{
scanf("%s",s);
for(int j = ; j<; j++)
{
if(s[j]=='+') a[i*+j] = ;
else a[i*+j] = ;
}
}
int i;
for(i = ; i<=; i++)
{
if(dfs(,,i)) break;
}
printf("%d\n",i);
for(int k = ; k<i; k++)
{
if(ans[k]!=-)
printf("%d %d\n",ans[k]/+, ans[k]%+);
} return ;
}

bfs:(bfs的队列可能不够大,溢出。用stl的话又怎么记录路径呢?)

 #include<cstdio>//poj 2965 bfs 未过
#include<cstring>
#include<queue> using namespace std; int ss,vis[],pre[];
struct node
{
int status, step,loc;
};
node q[]; void calcul( node *next,int i)
{
if(next->status&(<<i)) next->status -= (<<i);
else next->status += (<<i);
} void turn (node *next,int i)
{
calcul(next,i);
int xi = i/, yi = i%;
for(int k = ; k<; k++)
calcul(next,xi*+k);
for(int k = ; k<; k++)
calcul(next,k*+yi);
} int bfs()
{
int front = , rear = ;
q[front].status = ss, q[front].step = ;
vis[q[front].status] = ;
if(q[front].status==)
return front;
memset(vis,,sizeof(vis));
memset(pre,,sizeof(pre));
while(front<rear)
{
for(int i = ; i<; i++)
{
q[rear].status = q[front].status;
turn(&q[rear],i);
if(vis[q[rear].status]) continue; q[rear].loc = i;
q[rear].step = q[front].step + ;
pre[rear] = front;
vis[q[rear].status] = ; if(q[rear].status==) return rear;
rear++;
}
front++;
}
return -;
} void pri_path(int rear)
{
if(q[rear].step>) pri_path(pre[rear]);
printf("%d %d\n",q[rear].loc/+, q[rear].loc%+);
} int main()
{
int t;
char s[];
ss = ; for(int i = ; i<; i++)
{
scanf("%s",s);
for(int j = ; j<; j++)
{
if(s[j]=='+') ss += <<(i*+j);
}
}
int rear = bfs();
printf("%d\n",q[rear].step); pri_path(rear);
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 分析:1.这道题和之前做的poj1753题目差不多,常规思路也差不多,但是除了要输出最少步数外,还要输出路径.做这道题的时候在怎 ...

  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. Linux系统救援模式应用:单用户模式找回密码

    利用Linux系统救援模式找回密码 方法一: 开机时手要快按任意键,因为默认时间5s grub菜单,只有一个内核,没什么好上下选的,按e键.升级了系统或安装了Xen虚拟化后,就会有多个显示. 接下来显 ...

  2. C#中用鼠标移动页面功能的实现(代码控制滚动条)

    项目中需要实现以下功能: 打印预览控件中,可以用鼠标拖动页面,以查看超出显示范围之外的部分内容. 该功能本来可以通过拉动水平和垂直滚动条来实现,但实际使用中,用户更趋向于直接用鼠标拖动页面来实现,很多 ...

  3. 第十二题 Merge Sorted Array

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...

  4. 软件业的发展方向:云、Web以及App

    随着行业互联网的发展,未来的软件发展方向是云技术.Web软件以及基于移动设备的Apps. 桌面软件主要负责大型的计算.渲染和消耗非常大CPU和内存的图形软件,以及基于这些软件的二次开发软件如Revit ...

  5. leetcode第一刷_Word Search

    这道题之前一直没敢做,没想到前天用递归一遍过了. . 当时为什么想着用递归,而不是dp呢.由于我想到达某个位置的情况有非常多,即使从当前位置開始的搜索是已知的,但之前的状态是如何的也无从得知啊,实话实 ...

  6. hibernate映射排序

    @OneToMany(mappedBy="member") @OrderBy(value = "TousuID desc")

  7. LeetCode 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)

    翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致 ...

  8. 如何让DIV居中

    总结:text-align:center;对三中浏览器而言,都具有文字/行内元素的嵌套式居中,或者说继承式的居中,只要外面的容器设置了这个属性,那么他内部的所有元素都具有这个属性(意思是,虽然这个属性 ...

  9. p90x 涵盖了全部方式的健身方式美国经典训练DVD

    http://baike.baidu.com/view/2602721.htm  p90x是美国经典训练DVD, 涵盖了全部方式的健身方式13张Beachbody P90X DVD全集,90天魔鬼训练 ...

  10. Oracle在plsql中修改数据

    Oracle在plsql中想要修改数据,有两种方式: a.使用rowid+点击锁图标,语句为: select t.*,rowid from T_BIC_PLY_MAIN t;   b.使用for up ...