Problem UVA11694-Gokigen Naname

Accept: 76   Submit: 586
Time Limit: 10000 mSec

Problem Description

Input

The first line of the input file contains an integer N (N < 25) which denotes the total number of test cases. The description of each test case is given below: The first line of each test case contains a single integer n (2 ≤ n ≤ 7), the number of cells along each of the sides in the square grid. Then follow n+1 lines containing the contents of the intersections of the grid cells. Each such line will contain a string of n + 1 characters, either a digit between 0 and 4, inclusive, or a period (‘.’) indicating that there is no number at this intersection (arbitrarily many lines may connect to it).

 Output

For each test case print n lines, each line containing exactly n characters. Each character should either be a slash or a backslash, denoting how the corresponding grid cell is filled.

 

 Sample Input

2
3
1.1.
...0
.3..
..2.
5
.21...
..33.0
......
..33..
0..33.
....11
 

 Sample Output

\//
\\\
/\/
/\\//
//\\\
\\\//
\/\\/
///\\

题解:我感觉这个题挺难的,DFS的思路还比较直接,但是维护哪些东西比较迷茫。首先肯定有一个原图,然后要维护一下各点目前已经连了几条边,最关键的是要维护每个节点最多还能连几条边(用于剪枝)。判环的问题丢给并查集。DFS的时候从上到下,从左到右,每次连一条边更新维护的东西,要更新的东西比较多,回溯的时候别漏了。这里的并查集有必要提一句,这里不能路径压缩,因为回溯时是一种类似删除节点的操作,简单画个图就能发现如果只在回溯时修改当前节点的父节点为原来的父节点是不对的。因此这里不路径压缩,并查集基本上就是个链表的作用。剪枝有两个:1、如果一个节点连出的边数超了肯定要剪枝。2、利用lim数组,预估在最多的情况下能连几条边,如果少于目标边数,剪枝。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn = ;

 int n;
int gra[maxn][maxn], lim[maxn][maxn];
int cur[maxn][maxn], ans[maxn][maxn];
int dx[] = { ,,, };
int dy[] = { ,,, }; int pre[maxn*maxn]; int findn(int x) {
return x == pre[x] ? x : findn(pre[x]);
} inline bool ok(int x, int y) {
if (gra[x][y] == -) return true;
if (cur[x][y] <= gra[x][y] && cur[x][y] + lim[x][y] >= gra[x][y]) return true;
return false;
} bool dfs(int x, int y) {
if (y == n) x++, y = ;
if (x == n) return true; cur[x][y]++, cur[x + ][y + ]++;
lim[x][y]--, lim[x + ][y]--, lim[x][y + ]--, lim[x + ][y + ]--; bool can_put = true;
for (int i = ; i < ; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (!ok(xx, yy)) { can_put = false; break; }
}
if (can_put) {
int f1 = findn((x - )*n + y), f2 = findn(x*n + y + );
if (f1 != f2) {
ans[x][y] = ;
int tmp = pre[f2];
pre[f2] = f1;
if (dfs(x, y + )) return true;
pre[f2] = tmp;
}
} cur[x][y]--, cur[x + ][y + ]--;
cur[x][y + ]++, cur[x + ][y]++;
can_put = true;
for (int i = ; i < ; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (!ok(xx, yy)) { can_put = false; break; }
}
if (can_put) {
int f1 = findn(x*n + y), f2 = findn((x - )*n + y + );
if (f1 != f2) {
ans[x][y] = -;
int tmp = pre[f1];
pre[f1] = f2;
if (dfs(x, y + )) return true;
pre[f1] = tmp;
}
} cur[x][y + ]--, cur[x + ][y]--;
lim[x][y]++, lim[x + ][y]++, lim[x][y + ]++, lim[x + ][y + ]++;
return false;
} int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d", &n);
n++;
char ss[maxn];
for (int i = ; i <= n * n; i++) pre[i] = i;
memset(cur, , sizeof(cur)); for (int i = ; i <= n; i++) {
scanf("%s", ss + );
for (int j = ; j <= n; j++) {
if (ss[j] == '.') gra[i][j] = -;
else gra[i][j] = ss[j] - ''; lim[i][j] = ; if ((i == || i == n) && (j == || j == n)) {
lim[i][j] = ;
continue;
}
if (i == || j == || i == n || j == n) {
lim[i][j] = ;
}
}
} dfs(, ); for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
if (ans[i][j] == ) printf("\\");
else printf("/");
}
printf("\n");
}
}
return ;
}

UVA11694-Gokigen Naname(DFS进阶)的更多相关文章

  1. UVA11694 Gokigen Naname题解

    目录 写在前面 Solution Code 写在前面 UVA的题需要自己读入一个 \(T\) 组数据,别被样例给迷惑了 Solution 每个格子只有两种填法且 \(n \le 7\),暴力搜索两种填 ...

  2. 题解 UVA11694 【Gokigen Naname谜题 Gokigen Naname】

    题目 题解 考场上连暴力都不会打的码农题,深搜是真的难 /kk 前置问题 怎么输出"\" cout<<"\\"; 2.怎么处理不在一个环里,可以考虑 ...

  3. Uva 11694 Gokigen Naname

    基本思路是Dfs: 1. 一个一个格子摆放,以每个各自的左上角的点为基准点代表格子,比如(0,0)代表(0,0)(0,1)(1,0)(1,1)组成的格子,(0,1)代表(0,1)(0,2)(1,1), ...

  4. dfs进阶

    当自己以为自己深搜(其实就是暴力啦)小成的时候,发现没有题目的积累还是很难写出程序,自己真的是太年轻了:总结一下就是做此类题看是否需要使用vis数组优化以及继续搜索的条件或者满足答案的条件.以下为2题 ...

  5. hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. 0基础算法基础学算法 第八弹 递归进阶,dfs第一讲

    最近很有一段时间没有更新了,主要是因为我要去参加一个重要的考试----小升初!作为一个武汉的兢兢业业的小学生当然要去试一试我们那里最好的几个学校的考试了,总之因为很多的原因放了好久的鸽子,不过从今天开 ...

  7. 搜索进阶课件,视频,代码(状态压缩搜索,折半搜索,dfs,bfs总结)

    链接:https://pan.baidu.com/s/1-svffrprCOO4CtQoCTQ9hQ 提取码:h909 复制这段内容后打开百度网盘手机App,操作更方便哦

  8. ACM进阶计划

    ACM进阶计划ACM队不是为了一场比赛而存在的,为的是队员的整体提高.大学期间,ACM队队员必须要学好的课程有:lC/C++两种语言l高等数学l线性代数l数据结构l离散数学l数据库原理l操作系统原理l ...

  9. [转]ACM进阶计划

    ACM进阶计划  大学期间,ACM队队员必须要学好的课程有: lC/C++两种语言 l高等数学 l线性代数 l数据结构 l离散数学 l数据库原理 l操作系统原理 l计算机组成原理 l人工智能 l编译原 ...

随机推荐

  1. Android Studio 学习(一)

    XML给元素定义ID 定义id android:id="@+id/button_1" 引用id id/id_name 解决Failed to load Appcompat Acti ...

  2. python 实现微信自动回复(自动聊天)

    原文地址(本人):https://blog.csdn.net/a5878989/article/details/54974249 介绍 微信自动回复其实主要就是登录,接收消息,回复消息三个功能,微信没 ...

  3. POJ2484

    A Funny Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6178   Accepted: 3861 Desc ...

  4. 数据库sql语句常见面试题

    转载:本文转载自:https://blog.csdn.net/woshinidedege/article/details/78659202 一.有以下几张表及表结构Student(Sid,Sname, ...

  5. tpshop linux安装下注意事项

    1. 安装目录不可读写---赋予权限 chmod -Rf 777 public 2.安装环境参考 https://lnmp.org/install.html 3.wget 若没有安装 yum 安装

  6. gridcontrol 添加行删除行

    一.添加行 注意,必须先绑定数据源(可以是一个空的Datatable):然后再执行添加行的动作,否则不会成功: public partial class Form2 : Form { public F ...

  7. sublime text2 中标签高亮效果BracketHighlighter插件

    1.打开package Control,选择install Package 2.输入BracketHighlighter,回车 3.这样该插件会自动安装,安装后所有的提示高亮都是白色或没有提示.按 p ...

  8. bootstrap日期控件(双日期、清空等问题解决)

    bootstrap以它优美的外观和丰富的组件,使它成为目前最流行的前端框架.在项目开发中,我们使用它的日期控件确实遇到了一些问题: 1.日期控件后面两个图标点击触发失效 2.双日期关联问题 3.双日期 ...

  9. 照葫芦画瓢系列之Java --- Maven的配置

    一.Maven仓库分类 Maven中,仓库只分为两类:本地仓库和远程仓库.当Maven根据坐标寻找构件的时候,它首先去查看本地仓库,如果本地仓库有此构件,则直接使用,如果本地仓库不存在此构件,或者需要 ...

  10. RabbitMQ 消息流程、AMOP 概念

    AMOP Server:Broker.RabbitMQ Server,实现 AMOP 实体服务,接受客户端的连接 Conneciton:链接,应用程序与 Server 的网络连接 Channel:网络 ...