题目链接:

https://vjudge.net/problem/UVA-387

思路:

非常有意思的拼图,深搜+回溯,

输出硬伤:除了第一次之外,每次先输空格,再输出结果,

以及可能给的数据拼不成4*4表格的情况。

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 using namespace std;
5 int vis[5][5];
6 int g[10010][5][5];
7 int maze[10010][2];
8 int sum=0;
9 int n;
10 int Find=0,Case=0;
11 void dfs(int cur){
12 if(cur==n+1){//
13 Find=1;
14 for(int i=0;i<4;i++){
15 for(int j=0;j<4;j++){
16 printf("%d",vis[i][j]);
17 }
18 printf("\n");
19 }
20 return;
21 }
22 if(Find) return;
23 for(int i=0;i<=4-maze[cur][0];i++){//4*4表格起点横坐标
24 for(int j=0;j<=4-maze[cur][1];j++){//4*4表格起点纵坐标
25 int flag=0;//判重
26 for(int p=0;p<maze[cur][0];p++){
27 for(int q=0;q<maze[cur][1];q++){
28 if(!flag&&!vis[i+p][j+q]&&g[cur][p][q]==1)
29 vis[i+p][j+q]=cur;
30 else if(!flag&&g[cur][p][q]==0)
31 continue;
32 else
33 flag=1;
34 }
35 }
36 if(!flag) dfs(cur+1);
37
38 if(Find) return;
39 for(int p=0;p<4;p++){//重新扫描一遍表格,将上一次的清零
40 for(int q=0;q<4;q++){
41 if(vis[p][q]==cur)
42 vis[p][q]=0;
43 }
44 }
45 }
46 }
47 }
48 void init(){
49 memset(vis,0,sizeof(vis));
50 memset(g,0,sizeof(g));
51 memset(maze,0,sizeof(maze));
52 sum=0;
53 Find=0;
54 Case++;
55 }
56 int main(int argc, char** argv) {
57 while(scanf("%d",&n)!=EOF){
58 if(n==0) break;
59 init();
60
61 for(int i=1;i<=n;i++){
62 scanf("%d %d",&maze[i][0],&maze[i][1]);
63 string line;
64
65 for(int j=0;j<maze[i][0];j++){
66 cin>>line;
67 for(int k=0;k<maze[i][1];k++){
68 g[i][j][k]=line[k]-'0';
69 sum+=g[i][j][k];//有可能根本拼不成
70 }
71 }
72 }
73 if(Case!=1) printf("\n");
74 if(sum==16)
75 dfs(1);
76 if(!Find) printf("No solution possible\n");
77 }
78 return 0;
79 }

UVA - 387 A Puzzling Problem的更多相关文章

  1. uva 387 A Puzzling Problem (回溯)

     A Puzzling Problem  The goal of this problem is to write a program which will take from 1 to 5 puzz ...

  2. UVa 101 The Blocks Problem Vector基本操作

    UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...

  3. uva387 - A Puzzling Problem

    A Puzzling Problem The goal of this problem is to write a program which will take from 1 to 5 puzzle ...

  4. 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem

     UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...

  5. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  6. uva 10837 - A Research Problem(欧拉功能+暴力)

    题目链接:uva 10837 - A Research Problem 题目大意:给定一个phin.要求一个最小的n.欧拉函数n等于phin 解题思路:欧拉函数性质有,p为素数的话有phip=p−1; ...

  7. UVA 810 - A Dicey Problem(BFS)

    UVA 810 - A Dicey Problem 题目链接 题意:一个骰子,给你顶面和前面.在一个起点,每次能移动到周围4格,为-1,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地, ...

  8. UVA 10026 Shoemaker's Problem 鞋匠的难题 贪心+排序

    题意:鞋匠一口气接到了不少生意,但是做鞋需要时间,鞋匠只能一双一双地做,根据协议每笔生意如果拖延了要罚钱. 给出每笔生意需要的天数和每天的罚钱数,求出最小罚钱的排列顺序. 只要按罚款/天数去从大到小排 ...

  9. UVA 1640 The Counting Problem UVA1640 求[a,b]或者[b,a]区间内0~9在里面各个数的数位上出现的总次数。

    /** 题目:UVA 1640 The Counting Problem UVA1640 链接:https://vjudge.net/problem/UVA-1640 题意:求[a,b]或者[b,a] ...

随机推荐

  1. css 15-Sass入门

    15-Sass入门 #Sass简介 大家都知道,js 中可以自定义发量,css 仅仅是一个标记语言,不是编程语言,因此不可以自定义发量.不可以引用等等. 面对这些问题,我们现在来引入 Sass,简单的 ...

  2. js下 Day14、面向对象案例

    一.软键盘拖拽 效果图: ![img](file:////Users/sairitsutakara/Library/Group%20Containers/UBF8T346G9.Office/Tempo ...

  3. xss未看完的文章

    https://blog.csdn.net/fen0707/article/details/8596888                XSS介绍与攻击 http://xss.fbisb.com/w ...

  4. RabbitMQ史上最全文章

    老规矩,本篇文章 不做 RabbitMQ 的 编码讲解 ,只介绍 文章学习的一些优秀文章 重点在于不要循规蹈矩,教程 这样走,你不一定要按他这样走,按自己的方式来,学习效率会更高,网上的教程有很多,今 ...

  5. PHP简单的计算位数的函数

    一个简单的PHP计算位数的函数: 1 <?php 2 //一个简单的计算字符串有长度的函数 3 #开始定义函数 4 function count_digit($number){ 5 $count ...

  6. RHCE <复习RHSCA>

    什么是shell? shell是你(用户)和Linux(或者更准确的说,是你和Linux内核)之间的接口程序,你在提示符下输入的每个命令都由shell先解释然后传给Linux内核. bash 是大多数 ...

  7. 【磁盘/文件系统】第三篇:标准磁盘分区流程针对parted(一般硬盘容量大于2T(但是小于2T也可以进行分区);分区数最大是支持100多个分区)

    说明: 在 Linux 上可以采用 parted 来对磁盘进行分区 1.通过 fdisk -l 可以查看磁盘是否存在, 由于使用的是大磁盘(大于2T),fdisk 不能用来作为分区工具了,而应该使用 ...

  8. 实用干货!Java开发企业级权限管理系统视频教程

    全程手把手带你运用Java技术栈,打造一套基于最流行的RBAC拓展模型的,分布式的,有界面的,高灵活性,高拓展性的企业级权限管理系统.学完本课程你将可以轻松应对绝大多数企业开发中与权限管理及后台系统相 ...

  9. Kibana查询语言(KQL)

    一.前言 现在大多数的公司都会使用ELK组合来对日志数据的收集.存储和提供查询服务,这里就不介绍什么是ELK了,只介绍一些EKL中的查询,也就是K(kibana). 查询数据库,如果是MySQL,那么 ...

  10. ubuntu系统64位dnw

    /* dnw2 linux main file. This depends on libusb. * * * * Author: Fox <hulifox008@163.com> * * ...