ZOJ 1008 Gnome Tetravex(DFS)

果蔬及菜品识别系统 阅读详情

Gnome Tetravex

Time Limit: 10 Seconds Memory Limit: 32768 KB

Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given n*n squares. Each square is divided into four triangles marked four numbers (range from 0 to 9). In a square, the triangles are the left triangle, the top triangle, the right triangle and the bottom triangle. For example, Fig. 1 shows the initial state of 2*2 squares.


Fig. 1 The initial state with 2*2 squares

The player is required to move the squares to the termination state. In the termination state, any two adjoining squares should make the adjacent triangle marked with the same number. Fig. 2 shows one of the termination states of the above example.


Fig. 2 One termination state of the above example

It seems the game is not so hard. But indeed, Hart is not accomplished in the game. He can finish the easiest game successfully. When facing with a more complex game, he can find no way out.

One day, when Hart was playing a very complex game, he cried out, "The computer is making a goose of me. It's impossible to solve it." To such a poor player, the best way to help him is to tell him whether the game could be solved. If he is told the game is unsolvable, he needn't waste so much time on it.


Input

The input file consists of several game cases. The first line of each game case contains one integer n, 0 <= n <= 5, indicating the size of the game.

The following n*n lines describe the marking number of these triangles. Each line consists of four integers, which in order represent the top triangle, the right triangle, the bottom triangle and the left triangle of one square.

After the last game case, the integer 0 indicates the termination of the input data set.


Output

You should make the decision whether the game case could be solved. For each game case, print the game number, a colon, and a white space, then display your judgment. If the game is solvable, print the string "Possible". Otherwise, please print "Impossible" to indicate that there's no way to solve the problem.

Print a blank line between each game case.

Note: Any unwanted blank lines or white spaces are unacceptable.


Sample Input

2
5 9 1 4
4 4 5 6
6 8 5 4
0 4 4 3
2
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
0


Output for the Sample Input

Game 1: Possible

Game 2: Impossible


思路:先存不同方块的个数,然后挨个放,能放满就说明可能。否则就不可能。


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int mm=33;
class node
{
public:
  int u,r,d,l,n;
  node(){n=0;}
}f[mm];
int rs[mm];
int n,u,r,d,l,pos;
bool dfs(int x)
{
  if(x==n*n)return 1;
  for(int i=0;i<pos;i++)
  {
    if(x/n)
    { ///cout<<f[i].u<<" "<<f[i-n].d<<endl;
      if(f[i].u^f[rs[x-n]].d)continue;///符合上下
    }
    if(x%n)
    {///cout<<f[i].l<<" "<<f[i-1].r<<endl;
      if(f[i].l^f[rs[x-1]].r)continue;///符合左右
    }
    if(f[i].n==0)continue;
    f[i].n--;
    rs[x]=i;
    if(dfs(x+1))return 1;
    f[i].n++;
  }
  return 0;
}
int main()
{ int cas=0;
  while(cin>>n)
  {  pos=0;
    memset(f,0,sizeof(f));
    if(n==0)break;
    memset(rs,0,sizeof(rs));
    for(int i=0;i<n*n;i++)
    {
      cin>>u>>r>>d>>l;
       int j;
      for(j=0;j<pos;j++)
        if(u==f[j].u&&r==f[j].r&&d==f[j].d&&l==f[j].l)///存不同的方块
        {
          f[j].n++;break;
        }
        if(j==pos){f[pos].u=u;f[pos].r=r;f[pos].d=d;f[pos].l=l;f[pos].n++;pos++;}
    }
    if(cas)cout<<"\n";
    ++cas;
    cout<<"Game "<<cas<<": ";
    if(dfs(0))
      cout<<"Possible\n";
    else cout<<"Impossible\n";
  }
}



嵌入式工程师必学(118):MCU的硬件定时器和中断 大多数微处理器都有一个或多个可编程硬件定时器,这些定时器包括对中断的支持。可以有不同类型的计时器,用于不同的应用程序。例如,LPC1768 具有四个 32 位通用定时器、一个看门狗定时器、一个重复中断定时器、一个系统滴答定时器、一个正交编码器接口、6 个 PWM 输出和一个支持中断的实时时钟。硬件定时器用于计时和计数作,允许处理器在 timer 进程运行时继续执行其他进程。硬件定时器是一种使用 clock 信号来增加 register 的电路。 阅读详情

相关推荐

stm32f407VET6 系统学习 day01 GPIO 配置

stm32f407VET6 系统学习 day01 GPIO 配置 LED 蜂鸣器 按键的不讲了,之前讲过了

she666666的博客 8905

ZOJ--1008:Gnome Tetravex(剪枝dfs)

刚开始 做了一下 超时  然后看网上说要用 剪枝  有趣查了剪枝算法 结果 测试用例(自己写了好多测试用例)都可以通过 但是就是一直WA 想哭 import java.util.Scanner; public class Gnome_Tetravex { static int n,arr[][],put[],cnt[],cn,game; static boolean can;

yf224的博客 539

吉林大学2016级唐班c语言期末考试B卷

` #include<stdio.h> #include<stdlib.h> //双指针法,每次从a,b中选一个最大的数,从后向前一次放在数组a中 int merge(int *a,int m,int *b,int n){ int i=m-1,j=n-1,k=m+n-1; while(i>=0&&j>=0){ if(a[i]>b[j])a[...

weixin_42339173的博客 1195

浙大ZOJ 1008 Gnome Tetravex问题解决

不知怎么的,提交时老提示“Time Limit Exceeded”,但是在我自己机器上,我用clock()测量了下时间,当n为最大值5的时候,才200ms左右,不知为什么提交后,会显示100001ms。不管了,把代码贴在这里吧。 一、工程代码及算法设计注释 --------------------------------------------------gnome_tetrave

xiaogugood的专栏 1419

DS18B20温度传感器项目实战:从驱动编写到LCD显示,打造你的第一个温控系统

本文详细介绍了如何使用DS18B20温度传感器和LCD1602显示屏构建一个完整的温控系统。从硬件连接、OneWire协议实现到温度数据采集与LCD显示,全面解析了系统整合与工程化实现的关键步骤,帮助开发者快速掌握嵌入式温度监测系统的开发技巧。

weixin_42525328的博客 349

X3850x6 6241 fireware固件升级

登录联想https://datacentersupport.lenovo.com/cn/zc网站,选择system x,x3850 x6,type选择6241 选择驱动及应用程序,BIOS/UEFI 选择lnvgy_fw_uefi_a9e156a-4.90_anyos_32-64.uxz 文件,点击下载 选择IMM2, 下载lnvgy_fw_imm2_tcoo58b-5.80_anyos_noarch.uxz ps:3837和3839会跳转到ibm网站下载 笔记本直连.

zds00virtue的博客 5455

Gnome Tetravex

Gnome TetravexTime limit: 10 Seconds   Memory limit: 32768K   Total Submit: 1907   Accepted Submit: 397   Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at

Kinglliam的专栏 1709

机器人系统能用MQTT5.0代替ROS2吗?

更为灵活和友好的方案是通过把ROS(Robot Operating System)与MQTT(Message Queuing Telemetry Transport)协议相结合的解决方案,利用MQTT的轻量级、跨平台和广泛适用性特点,扩展ROS系统的通信能力,或将非ROS系统接入ROS网络。

QzmAIoT 2163

zoj 1008 Gnome Tetravex 【dfs】

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008 题意: 哈特近来一直在玩有趣的 Gnome Tetravex 游戏。在游戏开始时,玩家会得到 n×n(n≤5) 个正方形。每个正方形都被分成 4个标有数字的三角形(数字的范围是 0到9)。这四个三角形分 别被称为“左三角形”、“右三角形”、“上三角形”和“...

几许情愁的博客 288

【PySide6拓展】初识QChart图表

在 PySide6 中,QtCharts模块提供了一系列强大的类,用于创建和管理图表。QChart是这个模块的核心类,它可以帮助开发者轻松地实现数据的可视化。本篇文章将介绍如何使用 PySide6 的QtCharts模块,并通过一个简单的示例教你如何快速引入QChart。QChart折线图柱状图饼图区域图它能够直观地呈现数据,同时支持交互功能,如缩放、拖拽和工具提示等。通过引入QChart,我们可以轻松实现数据的可视化,提升应用的交互性和用户体验。结合其他QtCharts。

m0_62599305的博客 822

YOLO26中的PPM空间金字塔池化:改良设计与多尺度特征提取教程

在计算机视觉的语义分割任务中,准确理解图像中的上下文信息至关重要。例如,在街景图像中,可能同时存在远处的小汽车和近处的大建筑物,网络需要具备捕获多尺度上下文信息的能力。例如,要正确识别图像中的"船",不仅需要看到船本身的特征,还需要理解周围的水面环境。金字塔的每一层对应不同的池化尺度,从细粒度到粗粒度,形成了多层次的特征表示。PPM(Pyramid Pooling Module)正是为了解决这一问题而提出的模块,它通过构建金字塔形状的池化结构,有效地聚合不同尺度的上下文信息,从而提升语义分割的精度。

LXY_LOVE36的博客 82

【图的深度优先遍历搜索】ZOJ1008--Gnome Tetravex

【问题描述】 在N*N(0<=n<=5)的方格中,每一个方格有四个三角形按照上右下左的顺序次序排列,然后移动方格,是那些相邻的方格,满足相邻的对应三角形的值相等,有时候是无解的,有时候有解,题目就是让写下程序来判断给定一个方格看看是否能满足题意,如果能就打印possibble。 如果不能就打印impossible。 输入样例 2 5 9 1 4 4 4 5 6 6 8 5 4 0 4 4 3 2 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 输出样例 Game 1: Possi

Woo~ 866

Xilinx FPGA平台GTX简易使用教程(四)GTX IP核配置教程

干货来了,GTX IP核配置,搬砖全靠它~~

数字IC/FPGA杂货铺,找一找,或许有你想要的~ 1万+

ZOJ1008 Gnome Tetravex 暴力/DFS

Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given nn squares. Each square is divided into four triangles marked four numbers (range from 0 to 9). In a square, the triangles are th

ljw0925的博客 256

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

ZOJ 1008 Gnome Tetravex

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题目意思很简单,数据量也比较小,只需用dfs即可,不过还是需要剪纸,对于相同的类型的卡片记录数量,这样才不会超时。对于每一次放置的时候我们都要判断放置是不是合法,则个只需要通过一个简单的上左判断即可。程序如下: /* ID: chenan PROG: zoj 1008 Gn

BootStar 505

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

zoj 1008 Gnome Tetravex (dfs+枚举)

Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given

Coding A World 852

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字。现在要求重排方块,使得每两个有边相连的方块对应的数字相同。 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下

SIOFive的专栏 1265

ZOJ 1008 Gnome Tetravex(DFS)

Gnome TetravexTime Limit: 10 Seconds Memory Limit: 32768 KB Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given ...

a266676的博客 198

ZOJ 1008Gnome Tetravex DFS

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 zhuyi

GXwar の Code library 716

ZOJ1008 Gnome Tetravex(dfs)

Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given n

gyr 286

ZOJ1008 Gnome Tetravex DFS

URL:  Time Limit: 10 Seconds      Memory Limit: 32768 KB

王旻宇 590

ZOJ1008 Gnome Tetravex(DFS)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008 题意: 哈特近来一直在玩有趣的 Gnome Tetravex 游戏。在游戏开始时,玩家会得到 n×n(n≤5) 个正方形。每个正方形都被分成 4个标有数字的三角形(数字的范围是 0到9)。这四个三角形分 别被称为“左三角形”、“右三角形”、“上三角形”和

Keaper的博客 877

zoj1008 gnome tetravex 很久之前的dfs

嗯,第一次做时想了好久,后来找答案,发现正方形不能旋转。嗯,看错了。 #include #include int array [25][4]; int use[25]; int result[5][5]; bool flag; int n; bool can(int x,int y, int i) {                  if(x==0&&y==0)

XIAO! 495

ZOJ 1008 Gnome Tetravex (使用状态进行DFS)

Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is give

ZJUCSqxy的博客 391

ZOJ1008Gnome Tetravex(DFS)

Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is give

YCH1035235541的专栏 720

zoj 1008 Gnome Tetravex (dfs+剪枝)

Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is give

To be what you what to be! 1286
上一篇: CF-29C - Mail Stamps(DFS+离散化)
下一篇: ZOJ 1649 Rescue(BFS)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值