题意:给你一串数字序列,只包含0,1,2,一路扫描过去,遇到2则新开一个2x2的矩阵,然后如果扫到0或1就将其填入矩阵,注意不能四个方格全是0或者全是1,那样跟一个方格没区别,所以21111这种是不可能的,问根据串的数字先后顺序可不可能构造一个矩阵出来,正好把数字都填完,如果可以,输出该矩阵的大小,2^n*2^n的形式输出。

分析:其实就是递归求解,首先判断第一个数是不是2,不是则说明没有分块,所以长度必须是1,数字可以为0或1,如果是则dfs,每次遇到2,则dfs下一层,并从下一个下标开始,用flag[4]存储接下来的四个数,以判断是否出现一样的四个数。返回到第0层时,因为第一个数是2,所以j=0的时候就dfs了下去,所以全部符合的话,第0层的j+=1变为1,所以如果第0层j!=1,说明肯定有多的,返回false。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 2517 char ss[N];
int now,len,maxi; bool dfs(int dep)
{
now++; //位置移动
maxi = max(maxi,dep);
int j = ;
int tag[];
memset(tag,-,sizeof(tag));
for(j=;j<&&now<len;j++,now++)
{
tag[j] = ss[now]-'';
if(tag[j] != )
continue;
if(!dfs(dep+))
return ;
}
if(dep > )
{
if(tag[] == - || tag[] == - || tag[] == - || tag[] == -)
return ;
if(tag[] == tag[] && tag[] == tag[] && tag[] == tag[] && tag[] != ) //四个相同的组成一个
return ;
}
else if(j != ) //dep == 0
return ;
now--; //位置还原
return true;
} int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
scanf("%s",ss);
len = strlen(ss);
if(ss[] != '')
{
if(len != )
puts("Not Possible");
else
puts("2^0*2^0");
}
else
{
maxi = ;
now = -;
if(dfs())
printf("2^%d*2^%d\n",maxi,maxi);
else
puts("Not Possible");
}
}
return ;
}

UVA 12380 Glimmr in Distress --DFS的更多相关文章

  1. UVA.297 Quadtrees (四分树 DFS)

    UVA.297 Quadtrees (四分树 DFS) 题意分析 将一个正方形像素分成4个小的正方形,接着根据字符序列来判断是否继续分成小的正方形表示像素块.字符表示规则是: p表示这个像素块继续分解 ...

  2. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  3. Uva 552 Prime Ring Problem(dfs)

    题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...

  4. uva :10123 - No Tipping(dfs + 几何力矩 )

    option=com_onlinejudge&Itemid=8&page=show_problem&category=109&problem=1064&mosm ...

  5. UVA 11748 - Rigging Elections(dfs)

    UVA 11748 - Rigging Elections option=com_onlinejudge&Itemid=8&page=show_problem&category ...

  6. uva 699 The Falling Leaves dfs实现

    额,刘汝佳小白里面的配套题目. 题目求二叉树同垂直线上结点值的和. 可以用二叉树做,挺水的其实. 尝试使用dfs实现了:开一个大点的数组,根节点为最中间那点,然后读取时就可以进行和的计算了. 代码: ...

  7. UVA - 572 Oil Deposits(dfs)

    题意:求连通块个数. 分析:dfs. #include<cstdio> #include<cstring> #include<cstdlib> #include&l ...

  8. UVA 10400 Game Show Math (dfs + 记忆化搜索)

    Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...

  9. 【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。

    题意:给你一些任务1~n,给你m个数对(u,v)代表做完u才能做v 让你给出一个做完这些任务的合理顺序. 题解:拓扑排序版题 dfs到底再压入栈. #define _CRT_SECURE_NO_WAR ...

随机推荐

  1. Tomcat一些小事

    1.编码问题 1.1.乱码 客户端发请GET请求,如果这个请求地址上有中文,而且也没有进行encode的时候,后端就可能接收到乱码. --解决办法 在tomcat , conf/server.xml ...

  2. SVG基础图形与参数

    SVG是什么 SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG 用来定义WEB上使用的矢量图 SVG 使用 XML 格式定义图形 SVG 图像在缩放时其图形质量不 ...

  3. CSS3选择器(一)

    E[att^='val'] 选择属性值以val开头的任何字符 E[att$='val'] 选择属性值以val结尾的任何字符 E[att*='val'] 选择属性值包含val的任何字符 :root HT ...

  4. Convert part to feature command

    Search 库主页 Related Links ArcObjects SDK for ArcGIS 10Microsoft Help System Documentation Convert par ...

  5. andriod ==和equals

    == 用于数字 equals用于字符

  6. 2015第18本:从0到1,ZERO to ONE, Notes on startups, or how to build the future

    <从0到1>中文版的副标题是”开创商业与未来的秘密“,题目大得吓人,英文副标题就谨慎了许多:Notes on startups, or how to build the future. 全 ...

  7. Class org.apache.struts2.json.JSONWriter can not access a member of class org.springframework.aop.TruePointcut with modifiers "public"

    Spring注入Action使用Json错误:org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: ...

  8. iOS 利用CoreLocation和MapKit开发搜索附近的商场功能

    代码如下: ////  SearchNearbyShopViewController.m//  SearchNearbyShop////  Created by Linzhixiao on 16/2/ ...

  9. iOS之 PJSIP静态库编译(二)

    咱们书接上回: 上一篇编译好了PJsip这次我们来点实战 上次编译过后就不必做别的修改因为ios平台的库都支持了. 打开工程   找到 pjsip- apps/src/pjsua/ios/ipjsua ...

  10. const,static,extern简介(重要)

    一.const与宏的区别(面试题): const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 编译时刻:宏是预编译(编译之前处理),const是编译 ...