[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和个性化推荐的搜索排序优化
本文来自网易云社区 作者:穆学锋 简介:传统的搜索个性化做法是定义个性化的标签,将用户和商品通过个性化标签关联起来,在搜索时进行匹配.传统做法的用户特征基本是离线计算获得,不够实时:个性化标签虽然具有 ...
随机推荐
- js中数组Array的一些常用方法总结
var list = new Array()是我们在js中常常写到的代码,今天就总结一下Array的对象具有哪些方法. list[0] = 0; list[1] = 1; list[2] = 2; 或 ...
- ORACLE 常用字符函数
ORACLE 常用字符函数1 ASCII(arg1)返回参数arg1的十进制数字表示.如果数据库设置为ASCII,则采用的是ASCII码字符.如果设置为EBCDIC,则采用的是EBCDIC字符 sel ...
- mysql-master-ha
https://code.google.com/p/mysql-master-ha/wiki/TableOfContents?tm=6 http://www.cnblogs.com/gomysql/c ...
- css禁用鼠标点击事件
css禁用鼠标点击事件 .disabled { pointer-events: none; } <div class="main-container disabled"> ...
- [转]Hibernate3如何解决n+1 selects
摘自: http://blog.chinaunix.net/uid-20586655-id-287959.html Hibernate3中取得多层数据的所产生的n+1 selects问题的解决 ...
- 关于程序路径Path.Combine以及AppDomain.CurrentDomain.BaseDirectory
关于程序路径 LucenePath:@(System.Configuration.ConfigurationManager.AppSettings["LucenePath"])&l ...
- 剑指offer系列29-----链表中环的入口节点-
[题目]一个链表中包含环,请找出该链表的环的入口结点. [思路]方法一:使用双指针 方法二:利用set集合的特性,不能添加重复数字,否则返回false package com.exe7.offer; ...
- Python 通过print将数据保存到文件中
1. Print them to screen man = [] other = [] try: data = open('sketch.txt') for each_line in data: tr ...
- 【gradle】之maven主库找不到Could not find org.restlet.jee:org.restlet:2.1.1
Could not find org.restlet.jee:org.restlet:2.1.1. 我是用gradle构建solr的时候出现的这个错误,通过google查询到这么一段解释 For th ...
- [svn]svn merge
转:http://blog.csdn.net/keda8997110/article/details/21813035 Step by Step 完成merge 目录: Branch的必要性 1.本地 ...