BFS:UVa201-Squares
Squares
A children’s board game consists of a square array of dots that contains lines connecting some of the pairs of adjacent dots. One part of the game requires that the players count the number of squares of certain sizes that are formed by these lines. For example, in the figure shown below, there are 3 squares — 2 of size 1 and 1 of size 2. (The “size” of a square is the number of lines segments required to form a side.)
Your problem is to write a program that automates the process of counting all the possible squares.
Input
The input file represents a series of game boards. Each board consists of a description of a square array of n2 dots (where 2 ≤ n ≤ 9) and some interconnecting horizontal and vertical lines. A record for a single board with n2 dots and m interconnecting lines is formatted as follows:
Information for each line begins in column 1. The end of input is indicated by end-of-file. The first record of the sample input below represents the board of the square above.
Output
For each record, label the corresponding output with ‘Problem #1’, ‘Problem #2’, and so forth. Output for a record consists of the number of squares of each size on the board, from the smallest to the largest. lf no squares of any size exist, your program should print an appropriate message indicating so. Separate output for successive input records by a line of asterisks between two blank lines, like in the sample below.
Sample Input
4
16
H 1 1
H 1 3
H 2 1
H 2 2
H 2 3
H 3 2
H 4 2
H 4 3
V 1 1
V 2 1
V 2 2
V 2 3
V 3 2
V 4 1
V 4 2
V 4 3
2
3
H 1 1
H 2 1
V 2 1
Sample Output
Problem #1
2 square (s) of size 1 1 square (s) of size 2
“************************”(无引号)
Problem #2
No completed squares can be found.
解题心得
- 说实话被这个题给恶心惨了,在输出上为难人就算了,还要在输入描述上坑人,H i j 和 V i j 其中i并不是都是表示行,j都是表示列,坑人的啊。
- 具体的做法很简单,毕竟数据这么小,直接暴搜。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 15;
bool maps[maxn][maxn][maxn][maxn];
//maps[i][j][x][y]为真的时候代表点(i,j)到(x,y)之间有连线
int ans[maxn];
void get_ans(int n)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
int len = 1;
while(i+len<=n && j+len<=n)//从每一个点直接搜索,扩展长度就可以了
{
bool flag = false;
for(int k=0;k<len;k++)
if(!maps[i][j+k][i][j+k+1])
flag = true;
for(int k=0;k<len;k++)
if(!maps[i+len][j+k][i+len][j+k+1])
flag = true;
for(int k=0;k<len;k++)
if(!maps[i+k][j][i+k+1][j])
flag = true;
for(int k=0;k<len;k++)
if(!maps[i+k][j+len][i+k+1][j+len])
flag = true;
if(!flag)
ans[len]++;
len++;
}
}
bool flag = false;
for(int i=1;i<=n;i++)
if(ans[i])
flag = true;
if(!flag)
printf("No completed squares can be found.\n");
else
{
for(int i=1;i<=n;i++)
if(ans[i])
printf("%d square (s) of size %d\n",ans[i],i);
}
}
int main()
{
int n,t;
t = 1;
bool flag = false;
while(scanf("%d",&n) != EOF)
{
if(flag)
printf("\n**********************************\n\n");
flag = true;
printf("Problem #%d\n\n",t++);
memset(maps,0,sizeof(maps));
memset(ans,0,sizeof(ans));
int num;
scanf("%d",&num);
while(num--)
{
char ch[5];
int s,e;
scanf("%s%d%d",ch,&s,&e);
if(ch[0] == 'H')
maps[s][e][s][e+1] = true;
else
maps[e][s][e+1][s] = true;
}
get_ans(n);
}
return 0;
}
BFS:UVa201-Squares的更多相关文章
- uva201 Squares
Squares A children's board game consists of a square array of dots that contains lines connecting ...
- [刷题]算法竞赛入门经典(第2版) 4-2/UVa201 - Squares
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,20 ms) #include<iostream> #include<cs ...
- [LUOGU2730] 魔板
搜索水题.因为只有8个数,排列一共有40320种,直接bfs,判重就行了. 但是判重的时候直接用8进制表示的话要88的bool数组.这种操作太low了,于是我们可以用康托展开,降成8!. 康托展开其实 ...
- UVALive 4025 Color Squares(BFS)
题目链接:UVALive 4025 Color Squares 按题意要求放带有颜色的块,求达到w分的最少步数. //yy:哇,看别人存下整个棋盘的状态来做,我什么都不想说了,不知道下午自己写了些什么 ...
- 哈希+Bfs【P2730】 魔板 Magic Squares
没看过题的童鞋请去看一下题-->P2730 魔板 Magic Squares 不了解康托展开的请来这里-->我这里 至于这题为什么可以用康托展开?(瞎说时间到. 因为只有8个数字,且只有1 ...
- Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares)
Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...
- USACO3.25Magic Squares(bfs)
/* ID: shangca2 LANG: C++ TASK: msquare */ #include <iostream> #include<cstdio> #include ...
- (BFS) leetcode 279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- 洛谷 - P2730 - 魔板 Magic Squares - bfs
写状态转移弄了很久,老了,不记得自己的数组是怎么标号的了. #include <bits/stdc++.h> using namespace std; #define ll long lo ...
- 【习题 4-2 Uva201】Squares
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 注意那个星号的数量... 然后V x y的话,是从(y,x)向(y+1,x)连线. H x y才是从(x,y)向(x,y+1)连线 ...
随机推荐
- 《javascript设计模式》笔记之第五章:单体模式
一:单体的基本结构: 最简单的单体,实际就是一个对象字面量: var Singleton = { attribute1: true, attribute2, method1: function() { ...
- 我的NopCommerce之旅(4): 定时任务之邮件
一.功能简介 用户购买物品生成订单后,系统将发送邮件提醒给用户 二.操作步骤 后台配置一个系统的默认发送邮箱 启动定时任务,这里包括多个任务,只需要启动邮件任务 查看邮件发送情况 三.数据库分析 [d ...
- ASP.Net MVC 控制@Html.DisplayFor日期显示格式
在做一個舊表的查詢頁時,遇到一個問題: 字段在db里存儲的是DATETIME,但保存的值只有日期,沒有時間數據,比如2018/2/26 0:00:00,顯示出來比較難看, 當然也可以做一個ViewMo ...
- 编译运行第一个Java程序——通过示例学习Java编程3
作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=13 在本教程中,我们将了解如何编写.编译和运行Ja ...
- IDEA JavaSE环境配置
需指定JDK路径: File -> Project Structure -> Project -> Project SDK -> New -> 选择JDK所在的根目录
- Java开发笔记(九十六)线程的基本用法
每启动一个程序,操作系统的内存中通常会驻留该程序的一个进程,进程包含了程序的完整代码逻辑.一旦程序退出,进程也就随之结束:反之,一旦强行结束进程,程序也会跟着退出.普通的程序代码是从上往下执行的,遇到 ...
- [转]Java 8 Optional类深度解析(null处理)
原文链接:http://www.importnew.com/6675.html 本文由 ImportNew - 高俊阳 翻译自 javacodegeeks.欢迎加入翻译小组.转载请见文末要求. 身为一 ...
- C#调用CMD程序
最近写了两个小程序都要调用Windows自带的命令行程序,一个是调用Openfiles.exe查询正在编辑的共享文档,一个是调用DiskPart.exe查询硬盘状态.两种命令行程序调用有点不同,记录一 ...
- sping IOC的设计原理和高级特性
1. IOC 是Spring的内核,字面意思是控制反转,并提出了DI依赖注入的概念. 2.Spirng 容器的设计中,一个是实现BeanFactory 接口的简单饿汉容器,另外一个是比较高级的Appl ...
- JSONP 跨域请求 - 获取JSON数据
如何用原生方式使用JSONP? 下边这一DEMO实际上是JSONP的简单表现形式,在客户端声明回调函数之后,客户端通过script标签向服务器跨域请求数据,然后服务端返回相应的数据并动态执行回调函数. ...