Easy Finding POJ - 3740(十字交叉双向循环链表dancing links)

举报
用户已注销 发表于 2021/11/19 04:58:21 2021/11/19
【摘要】 题目: Given a  M×  N matrix  A.  A  ij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn con...

题目:

Given a  M×  N matrix  AA  ij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1.
There are multiple cases ended by  EOF. Test case up to 500.The first line of input is  MN (  M ≤ 16,  N ≤ 300). The next  M lines every line contains  N integers separated by space.
For each test case, if you could find it output "Yes, I found it", otherwise output "It is impossible" per line.
3 3
0 1 0
0 0 1
1 0 0
4 4
0 0 0 1
1 0 0 0
1 1 0 1
0 1 0 0
Yes, I found it
It is impossible


单纯的精确覆盖问题,用来作为学习dancing links的第一个题目再合适不过了。

代码:


  
  1. #include<iostream>
  2. #include<stdio.h>
  3. using namespace std;
  4. int m, n, num[17][301];
  5. const int maxx = 6000, mr = 17;
  6. int key, up[maxx], down[maxx], lef[maxx], rig[maxx];//上下左右
  7. int row[maxx], col[maxx], rhead[mr];//行,列,每行第一个元素
  8. void init()
  9. {
  10. for (int i = 0; i <= n; i++)
  11. {
  12. up[i] = i, down[i] = i;
  13. lef[i] = i - 1, rig[i] = i + 1;
  14. row[i] = 0, col[i] = i;
  15. }
  16. lef[0] = n, rig[n] = 0;
  17. key = n;
  18. for (int i = 0; i <= m; i++)rhead[i] = 0;
  19. }
  20. void push(int r, int c)//新增坐标在(r,c)的一个节点
  21. {
  22. row[++key] = r, col[key] = c;
  23. up[key] = c, down[key] = down[c];
  24. up[down[c]] = key, down[c] = key;
  25. if (rhead[r] == 0)rhead[r] = lef[key] = rig[key] = key;
  26. else
  27. {
  28. lef[key] = rhead[r], rig[key] = rig[rhead[r]];
  29. lef[rig[rhead[r]]] = key, rig[rhead[r]] = key;
  30. }
  31. }
  32. void del(int c)//删除第c列的所有元素和他们所在行的所有元素
  33. {
  34. lef[rig[c]] = lef[c], rig[lef[c]] = rig[c];
  35. for (int i = down[c]; i != c; i = down[i])
  36. for (int j = rig[i]; j != i; j = rig[j])
  37. down[up[j]] = down[j], up[down[j]] = up[j];
  38. }
  39. void reback(int c)
  40. {
  41. lef[rig[c]] = rig[lef[c]] = c;
  42. for (int i = down[c]; i != c; i = down[i])
  43. for (int j = rig[i]; j != i; j = rig[j])
  44. down[up[j]] = up[down[j]] = j;
  45. }
  46. bool dfs()
  47. {
  48. if (rig[0] == 0)return true;
  49. int c = rig[0];
  50. del(c);
  51. for (int i = down[c]; i != c; i = down[i])
  52. {
  53. for (int j = rig[i]; j != i; j = rig[j])del(col[j]);
  54. if (dfs())return true;
  55. for (int j = rig[i]; j != i; j = rig[j])reback(col[j]);
  56. }
  57. reback(c);
  58. return false;
  59. }
  60. int main()
  61. {
  62. while (scanf("%d%d",&m,&n)!=EOF)
  63. {
  64. init();
  65. for (int i = 1; i <= m; i++)for (int j = 1; j <= n; j++)
  66. {
  67. scanf("%d", &num[i][j]);
  68. if (num[i][j])push(i, j);
  69. }
  70. if (dfs())printf("Yes, I found it\n");
  71. else printf("It is impossible\n");
  72. }
  73. return 0;
  74. }

文章来源: blog.csdn.net,作者:csuzhucong,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nameofcsdn/article/details/79890911

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。