题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008

题目大意:给你n*n的矩阵,每个格子里有4个三角形,分别是上右下左,每个三角形里面标记了数字,问你能否通过移动这n*n个格子,使得相邻两个三角形具有相同的数字?

dfs暴搜,dfs(x,y)代表当前要放(x,y)这个位置。

然后枚举给定的每个格子。

如果说可以放,就dfs下一个格子。

这一道题需要把相同的格子压缩起来,也就是说为了节省时间,可以记录相同的格子出现的次数。

然后对于这不同种的格子去dfs。

如果不这样做会超时。

这算是一种优化方法吧。涨姿势了。

 #include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <functional>
#include <cmath>
#include <numeric>
#include <ctime>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define PB push_back
#define MP make_pair
#define SZ size()
#define CL clear()
#define AA first
#define BB second
#define EPS 1e-8
#define ZERO(x) memset((x),0,sizeof(x))
const int INF = ~0U>>;
const double PI = acos(-1.0); int n;
int now[][];
bool vis[];
int ptr;
bool flag; struct Node{
int top,right,bottom,left;
int num;
bool operator==(const Node& b) const{
return top==b.top&&right==b.right&&bottom==b.bottom&&left==b.left;
}
Node(int t=,int r=,int b=,int l=):top(t),right(r),bottom(b),left(l){}
};
Node mp[]; void dfs(int x,int y){
if(flag) return;
if( x==n || y==n ){
flag = true;
return;
}
for(int i=;i<ptr;i++){
if( mp[i].num&&
(x==||mp[now[x-][y]].bottom==mp[i].top)
&&(y==||mp[now[x][y-]].right==mp[i].left) ){
now[x][y] = i;
mp[i].num--;
if( y+<n ) dfs(x,y+);
else dfs(x+,);
mp[i].num++;
}
}
} int main(){
int kase = ;
while(scanf("%d",&n),n){
ptr = ;
for(int i=;i<n*n;i++){
int t,r,b,l;
scanf("%d%d%d%d",&t,&r,&b,&l);
bool appear = ;
for(int j=;j<ptr;j++){
if(Node(t,r,b,l)==mp[j]){
mp[j].num++;
appear = ;
}
}
if(!appear){
mp[ptr] = Node(t,r,b,l);
mp[ptr++].num = ;
}
} flag = false;
dfs(,); if(kase!=) puts("");
printf("Game %d: ",kase++);
puts(flag?"Possible":"Impossible");
}
return ;
}

[ZOJ 1008]Gnome Tetravex (dfs搜索 + 小优化)的更多相关文章

  1. ZOJ 1008 Gnome Tetravex(DFS)

    Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an inte ...

  2. ZOJ 1008 Gnome Tetravex(DFS)

    题目链接 题意 : 将n*n个正方形进行排列,需要判断相邻的正方形的相邻三角形上边的数字是不是都相等. 思路 : 只知道是个深搜,一开始不知道怎么搜,后来看了题解才明白,就是说不是自己去搜,而是将给定 ...

  3. zoj 1008 Gnome Tetravex

    开放式存储阵列为每平方米有几个,否则,超时-- #include <stdio.h> #include <string.h> #include <iostream> ...

  4. [ZOJ 1003] Crashing Balloon (dfs搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3 题目大意:给你a,b两个数,问当b由约数1到100组成时,a能否由其 ...

  5. 1008 Gnome Tetravex

    练习使用DPS的题,不知道有无别的做法,思路不复杂.形式是统计并且进行数字配对. #include <stdio.h> ][],note[],ans[]; void ini(){ int ...

  6. nyist oj 19 擅长排列的小明(dfs搜索+STL)

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 小明十分聪明.并且十分擅长排列计算.比方给小明一个数字5,他能立马给出1-5按字典序的全排列,假设你想 ...

  7. [ZOJ 1011] NTA (dfs搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1011 题目大意:在一棵树上,给你起始状态,问你能否到达终止状态. ...

  8. hihocoder 1050 树中的最长路(动态规划,dfs搜索)

    hihocoder 1050 树中的最长路(动态规划,dfs搜索) Description 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅 ...

  9. 搜索实时个性化模型——基于FTRL和个性化推荐的搜索排序优化

    本文来自网易云社区 作者:穆学锋 简介:传统的搜索个性化做法是定义个性化的标签,将用户和商品通过个性化标签关联起来,在搜索时进行匹配.传统做法的用户特征基本是离线计算获得,不够实时:个性化标签虽然具有 ...

随机推荐

  1. maven 常见错误解决

    1. maven打包编译时后台一直输出警告信息 [WARNING] File encoding has not been set, using platform encoding GBK, i.e. ...

  2. ORA-01109:数据库未打开

    ORA-01109:数据库未打开 在此之前做了这样一操作,在plsql创建了2表空间,由于装的是oracle精简版所以创建表空间大小超过4G就不能创建,然后我就手动把表空间给删除了,回收站也给删了,问 ...

  3. NAT,网络地址转换详解

    这个技术,是一个非常成熟的技术了,但是,为了将其弄得清楚点,体系点,也为了备忘,还是有必要在这里梳理一下! NAT:Network Address Translation. 这个主要是用在网络地址(I ...

  4. 用Opera Mobile调试手机版网页【转】

    注意:新版本的opera已经采用webkit内核,没有dragonfly了. 要下载12版的http://get.geo.opera.com/pub/opera/win/1216/int/Opera_ ...

  5. [Hibernate] - one to many

    事实上one to many 和 many to one是一样的,这是一个相互的过程. hibernate.cfg.xml <?xml version="1.0" encod ...

  6. css之四大类选择器

    一.选择器: 关于组合选择器之间的区别: 后代选择器(A B)与子选择器(A>B)之间的区别: 后代选择器: 子选择器: 总结:后代选择器的作用范围是一个元素里面包含的所有元素,即包括的是所有的 ...

  7. Maven仓库—Nexus环境搭建及简单介绍

    1.    环境搭建 1.1  下载 http://www.sonatype.org/nexus/ NEXUS OSS [OSS = Open Source Software,开源软件--免费] NE ...

  8. 如何缩短SQL Server 的启动时间

    将/nosplash添加到SQLServer Manageement Studio的快捷方式可以缩短启动时间.为此,右击SSMS快捷方式(位于你的桌面上.start菜单中或任务栏中)并选择Proper ...

  9. 兼容所有浏览器---无缝上下左右交叉运动----原生js+css

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. 《黄聪:手机移动站SEO优化教程》3、如何禁止百度对PC网站进行自动转码

    视频地址:http://v.youku.com/v_show/id_XNzE2OTM0NzU2.html