[ZOJ 1008]Gnome Tetravex (dfs搜索 + 小优化)
题目链接: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搜索 + 小优化)的更多相关文章
- ZOJ 1008 Gnome Tetravex(DFS)
Gnome Tetravex Time Limit: 10 Seconds Memory Limit: 32768 KB Hart is engaged in playing an inte ...
- ZOJ 1008 Gnome Tetravex(DFS)
题目链接 题意 : 将n*n个正方形进行排列,需要判断相邻的正方形的相邻三角形上边的数字是不是都相等. 思路 : 只知道是个深搜,一开始不知道怎么搜,后来看了题解才明白,就是说不是自己去搜,而是将给定 ...
- zoj 1008 Gnome Tetravex
开放式存储阵列为每平方米有几个,否则,超时-- #include <stdio.h> #include <string.h> #include <iostream> ...
- [ZOJ 1003] Crashing Balloon (dfs搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3 题目大意:给你a,b两个数,问当b由约数1到100组成时,a能否由其 ...
- 1008 Gnome Tetravex
练习使用DPS的题,不知道有无别的做法,思路不复杂.形式是统计并且进行数字配对. #include <stdio.h> ][],note[],ans[]; void ini(){ int ...
- nyist oj 19 擅长排列的小明(dfs搜索+STL)
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 小明十分聪明.并且十分擅长排列计算.比方给小明一个数字5,他能立马给出1-5按字典序的全排列,假设你想 ...
- [ZOJ 1011] NTA (dfs搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1011 题目大意:在一棵树上,给你起始状态,问你能否到达终止状态. ...
- hihocoder 1050 树中的最长路(动态规划,dfs搜索)
hihocoder 1050 树中的最长路(动态规划,dfs搜索) Description 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅 ...
- 搜索实时个性化模型——基于FTRL和个性化推荐的搜索排序优化
本文来自网易云社区 作者:穆学锋 简介:传统的搜索个性化做法是定义个性化的标签,将用户和商品通过个性化标签关联起来,在搜索时进行匹配.传统做法的用户特征基本是离线计算获得,不够实时:个性化标签虽然具有 ...
随机推荐
- Ext is not defined
最近由于项目设计到Extjs所以也准备研究一下,可是谁知道刚写好一个demo,运行的时候死活出不来界面,于是用firebug看了一下,出现:Ext is not defined,因为刚开始学也不知道是 ...
- Deferred和Promise之间有什么区别呢?
一个promise就是一个由异步函数返回的对象. deferred对象就是jQuery的回调函数解决方案. 总结 jQuery 的ajax 就是返回一个promise 对象,里面含有done(), f ...
- js停止冒泡和阻止浏览器默认行为
停止冒泡通用方法: function stopBubble(e) { //如果提供了事件对象,是非IE浏览器 if ( e && e.stopPropagation ) //使用W3C ...
- MyEclipse配置Resin启动报错问题
错误信息如下: com.caucho.config.ConfigException: -server 'default' is an unknown server in the configurati ...
- 微信支付(0923更新)商户支付密钥key的生成与设置
微信支付(0923更新)商户支付密钥key的生成与设置 说明:新版微信支付,用户必须授权登录才能支付.需要商家自己设置商户号支付密钥. 设置商户号支付密钥方法如下: 1. 申请通过审核后,打开微信发来 ...
- HDP2.4安装(六):小结与回顾
基于Centos7安装过程中常用工具及操作技术总结回顾. 操作技巧: tab键,命令自动补全 xshell 默认: Ctrl + Insert (复制) Shift + Insert (粘贴) sy ...
- Struts2 Interceptors
Alias Interceptor : 别名拦截器 <action name="someAction" class="com.examples.SomeAction ...
- HTML5外包团队——技术分享:HTML5判断设备在线离线及监听网络状态变化例子
<!doctype html> <html> <head> <meta http-equiv="content-type" content ...
- CentOS6.8安装Redis3.2.5
1.下载Redis3.2.5安装包 wget http://download.redis.io/releases/redis-3.2.5.tar.gz 2.解压.编译.安装redi ...
- 151. Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...