UVA11694 Gokigen Naname题解
写在前面
UVA的题需要自己读入一个 \(T\) 组数据,别被样例给迷惑了
Solution
每个格子只有两种填法且 \(n \le 7\),暴力搜索两种填法,开cnt数组统计连接个数。
填一个格子,如果是 "\",格子左上角和右下角的 \(cnt++\),如果是 "/",格子左下角和右上角的 \(cnt++\)。只有更改后的 \(cnt\) 小于等于目标 \(cnt\)才继续向下搜,否则回溯
发现每填一个格子,连接格子左上角的格点的个数就可以被确定,那么只有左上角的格点个数等于目标格点个数或没有要求才继续搜索,否则回溯
如果搜索到第 \(n + 1\) 列时,要额外判断边界第 \(n + 1\) 列的 \(cnt\)是否满足对应的个数
如果搜索到第 \(n\) 行时,要额外判断边界第 \(n + 1\) 行的 \(cnt\) 是否满足对应个数
如何保证无环?不难想到,只有在填 "/" 时才有可能出现环,那么在填 "/" 之前,先判断是否有环
如何处理点的坐标?把行看做十位,把列看做个位就好啦
算法一:考虑可撤销并查集,然而我不会
算法二:发现n很小,每次判断时 \(n^2\) 扫一遍建图,在 \(dfs\) 看看能否从左下角跑到右上角
算法三:很显然算法二很傻逼,直接用并查集维护就好,加完边后判断左下角和右上角是否在同一并查集里,省去 \(dfs\) 的时间,注意清零!
最后输出方案就好啦
Code
我一开始建图跑的,后来也没改(因为懒
/*
Work by: Suzt_ilymics
Knowledge: ??
Time: O(??)
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define orz cout<<"lkp AK IOI!"<<endl
using namespace std;
const int MAXN = 10;
const int INF = 1;
const int mod = 1;
struct edge{
int from, to, nxt;
}e[10100 << 1];
int head[MAXN * MAXN], num_edge = 0;
int n;
int go[MAXN][MAXN];
int cnt[MAXN][MAXN], now[MAXN][MAXN];
bool flag = false, Flag = false;
int read(){
int s = 0, f = 0;
char ch = getchar();
while(!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while(isdigit(ch)) s = (s << 1) + (s << 3) + ch - '0' , ch = getchar();
return f ? -s : s;
}
void add_edge(int from, int to){
e[++num_edge] = (edge){from, to, head[from]}, head[from] = num_edge;
}
bool bl(int u, int fa, int end){
for(int i = head[u]; i != -1; i = e[i].nxt){
int v = e[i].to;
if(v == fa) continue;
if(v == end) {
Flag = 1;
return true;
}
bl(v, u, end);
if(Flag) { return true; }
}
return false;
}
bool pd(int sx, int sy, int ex, int ey){
memset(head, -1, sizeof(head)); num_edge = 0;
Flag = false;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(now[i][j] == 0){
add_edge(i * 10 + j, (i + 1) * 10 + j + 1);
add_edge((i + 1) * 10 + j + 1, i * 10 + j);
}
if(now[i][j] == 1){
add_edge((i + 1) * 10 + j, i * 10 + j + 1);
add_edge(i * 10 + j + 1, (i + 1) * 10 + j);
}
}
}
if(bl(sx * 10 + sy, 0, ex * 10 + ey)) return 1;
else return 0;
}
void dfs(int posx, int posy){
// cout<<posx<<" "<<posy;
// orz;
if(posx == n && posy == n + 1) {
if( ((cnt[posx + 1][posy] == go[posx + 1][posy]) || (go[posx + 1][posy] == 9)) && ((cnt[posx][posy] == go[posx][posy]) || (go[posx][posy] == 9)) ) flag = 1;
return ;
}// 如果搜到最后一个点,说明已经找到答案。退出
if(posy == n + 1){ //如果这一行搜完了
if((cnt[posx][posy] == go[posx][posy]) || (go[posx][posy] == 9) ) posx++, posy = 1;//换行
else return ;// 如果已经确定的那个数并没有达到目标,返回
}//
now[posx][posy] = 0;// 填 "\"
cnt[posx][posy]++, cnt[posx + 1][posy + 1]++;// 对应位置加1
if(cnt[posx][posy] <= go[posx][posy] && cnt[posx + 1][posy + 1] <= go[posx + 1][posy + 1]) {//如果两个对应位置大于目标位置就不向下搜索
if((go[posx][posy] != 9 && go[posx][posy] == cnt[posx][posy]) || (go[posx][posy] == 9)) {//如果已经确定的那个数没有达到目标,停止向下搜索
if((posx != n) || (posx == n && ( (go[posx + 1][posy] != 9 && go[posx + 1][posy] == cnt[posx + 1][posy]) || (go[posx + 1][posy] == 9) ) )){
dfs(posx, posy + 1);//
}
}
}//
if(flag) return ;// 如果找到答案就返回
cnt[posx][posy]--, cnt[posx + 1][posy + 1]--;//回溯
if(pd(posx + 1, posy, posx, posy + 1)){ return ;}
now[posx][posy] = 1;// 填 "/"
cnt[posx + 1][posy]++, cnt[posx][posy + 1]++;// 对应位置加1
if(cnt[posx + 1][posy] <= go[posx + 1][posy] && cnt[posx][posy + 1] <= go[posx][posy + 1]) {//如果两个对应位置大于目标位置就不向下搜索
if((go[posx][posy] != 9 && go[posx][posy] == cnt[posx][posy]) || (go[posx][posy] == 9)) {//如果已经确定的那个数没有达到目标,停止向下搜索
if((posx != n) || (posx == n && ( (go[posx + 1][posy] != 9 && go[posx + 1][posy] == cnt[posx + 1][posy]) || (go[posx + 1][posy] == 9) ) )){
dfs(posx, posy + 1);//
}
}
}
if(flag) return ;// 如果找到答案就返回
now[posx][posy] = -1;//回溯
cnt[posx + 1][posy]--, cnt[posx][posy + 1]--;// 回溯
}
int main()
{
// freopen("gokigen.in","r",stdin);
// freopen("gokigen.out","w",stdout);
int T;
T = read();
while(T--){
n = read();
memset(now, -1, sizeof(now));
memset(cnt, 0, sizeof(cnt));
memset(go, 0, sizeof(go));
flag = 0;
char ch[10];
for(int i = 1; i <= n + 1; ++i){
cin>>(ch + 1);
for(int j = 1; j <= n + 1; ++j){
if(isdigit(ch[j])) go[i][j] = ch[j] - '0';
else go[i][j] = 9;
}
}
dfs(1, 1);
// for(int i = 1; i <= n + 1; ++i){
// for(int j = 1; j <= n + 1; ++j){
// cout<<go[i][j]<<" ";
// }
// cout<<"\n";
// }
// cout<<"\n";
//
// for(int i = 1; i <= n + 1; ++i){
// for(int j = 1; j <= n + 1; ++j){
// cout<<cnt[i][j]<<" ";
// }
// cout<<"\n";
// }
// cout<<"\n";
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(now[i][j] == 1) cout<<"/";
else if(now[i][j] == 0) cout<<"\\";
else cout<<"s";
}
cout<<"\n";
}
}
return 0;
}
UVA11694 Gokigen Naname题解的更多相关文章
- 题解 UVA11694 【Gokigen Naname谜题 Gokigen Naname】
题目 题解 考场上连暴力都不会打的码农题,深搜是真的难 /kk 前置问题 怎么输出"\" cout<<"\\"; 2.怎么处理不在一个环里,可以考虑 ...
- Uva 11694 Gokigen Naname
基本思路是Dfs: 1. 一个一个格子摆放,以每个各自的左上角的点为基准点代表格子,比如(0,0)代表(0,0)(0,1)(1,0)(1,1)组成的格子,(0,1)代表(0,1)(0,2)(1,1), ...
- UVA11694-Gokigen Naname(DFS进阶)
Problem UVA11694-Gokigen Naname Accept: 76 Submit: 586Time Limit: 10000 mSec Problem Description I ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
随机推荐
- [LeetCode]92. Reverse Linked List II反转部分链表
/* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...
- JS常见面试题,看看你都会多少?
1. 如何在ES5环境下实现let 这个问题实质上是在回答let和var有什么区别,对于这个问题,我们可以直接查看babel转换前后的结果,看一下在循环中通过let定义的变量是如何解决变量提升的问题 ...
- FTP服务器的搭建和使用(centos7)
1.显示如下图则表示已安装vsftp软件 如过没有则可以通过yum源进行安装 yum install -y vsftpd 操作:service vsftpd start|stop|restart 2. ...
- 你知道 react-color 的实现原理吗
一.前言 ReactColor 是一个优秀的 React 颜色选择器组件,官方给了多种布局供开发者选择. 笔者常用的主题为 Sketch,这种主题涵盖了颜色面板.推荐色块.RGB颜色输入等功能,比较完 ...
- chatsRoom Design Report
基于TCP实现聊天室 主要使用四个类 ChatClient类 使用BufferedReader 得到输入流,使用OutputStream得到输出流 实现读取服务器广播的消息和发送消息到 ...
- openstack octavia的实现与分析(一)openstack负载均衡的现状与发展以及lvs,Nginx,Haproxy三种负载均衡机制的基本架构和对比
[负载均衡] 大量用户发起请求的情况下,服务器负载过高,导致部分请求无法被响应或者及时响应. 负载均衡根据一定的算法将请求分发到不同的后端,保证所有的请求都可以被正常的下发并返回. [主流实现-LVS ...
- SpringBoot2.+restful风格请求方式设置以及表单中日期格式设置
1).SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean.@Component)如果有就用用户配置的,如果没有,才自动配置:如果有些组件可以有多个(ViewR ...
- Kubernetes学习笔记之认识Kubernetes组件
前言:笔记知识点来源于Kubernetes官方文档说明,链接:https://kubernetes.io/docs/concepts/overview/components/ ,本记录仅仅是学习笔记记 ...
- 修改hosts文件后不生效,该怎么办
对于web开发来说,经常需要修改hosts文件,用来将域名与ip对应匹配.但是有时候发现hosts文件明明已经改了,但就是不生效,页面还会跳到某个丧心病狂的私人小站.hosts文件不生效有很多种原因, ...
- 【Oracle】表空间配额问题
由于需求,需要新建用户,但是新建的用户,会有相关的配额跟着,莫名其妙的问题让人很头疼 下面介绍下如何修改成不限制配额 select * from user_ts_quotas ; alter user ...