zoukankan      html  css  js  c++  java
  • 【SGU 101】Domino

    101. Domino

    time limit per test: 0.5 sec.
    memory limit per test: 4096 KB

    Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The blocks usually are called bones, dominoes, or pieces and sometimes men, stones, or even cards.
    The face of each piece is divided, by a line or ridge, into two squares, each of which is marked as would be a pair of dice...

    The principle in nearly all modern dominoes games is to match one end of a piece to another that is identically or reciprocally numbered.

    ENCYCLOPÆDIA BRITANNICA

    Given a set of domino pieces where each side is marked with two digits from 0 to 6. Your task is to arrange pieces in a line such way, that they touch through equal marked sides. It is possible to rotate pieces changing left and right side.

    Input

    The first line of the input contains a single integer N (1 ≤ N ≤ 100) representing the total number of pieces in the domino set. The following N lines describe pieces. Each piece is represented on a separate line in a form of two digits from 0 to 6 separated by a space.

    Output

    Write “No solution” if it is impossible to arrange them described way. If it is possible, write any of way. Pieces must be written in left-to-right order. Every of N lines must contains number of current domino piece and sign “+” or “-“ (first means that you not rotate that piece, and second if you rotate it).

    Sample Input

    5
    1 2
    2 4
    2 4
    6 4
    2 1
    

    Sample Output

    2 -
    5 +
    1 +
    3 +
    4 - 

      开刷SGU!
      题目就是让你找一个欧拉路,没什么好说的,dfs搞定。
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #define mm 11000
    using namespace std;
    
    int n,a,b,degree[mm];
    bool vis[mm]={false};
    
    struct EDGE{
    	int pnt,idx;
    	EDGE *pre;
    	EDGE(){}
    	EDGE(int _pnt,int _idx,EDGE *_pre):pnt(_pnt),idx(_idx),pre(_pre){}
    }Edge[mm*2],*SP=Edge,*edge[mm];
    
    inline void addedge(int a,int b,int idx){
    	edge[a]=new(++SP)EDGE(b,idx,edge[a]);
    	edge[b]=new(++SP)EDGE(a,-idx,edge[b]);
    	degree[a]++,degree[b]++;
    }
    
    void dfs(int x){
    	for(EDGE *j=edge[x];j;j=j->pre)
    		if(!vis[abs(j->idx)]){
    			vis[abs(j->idx)]=true;
    			dfs(j->pnt);
    		}
    }
    
    void Output(int x){
    	for(EDGE *j=edge[x];j;j=j->pre)
    		if(!vis[abs(j->idx)]){
    			vis[abs(j->idx)]=true;
    			Output(j->pnt);
    			printf("%d %c\n",abs(j->idx),j->idx>0?'-':'+');
    		}
    }
    
    void Halt(){
    	printf("No solution\n");
    	exit(0);
    }
    
    int main(){
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++){
    		scanf("%d%d",&a,&b);
    		addedge(a,b,i);
    	}
    	int source=0,cnt=0;
    	for(int i=0;i<7;i++)
    		if(degree[i]&1) source=i,cnt++;
    	if(cnt && cnt!=2) Halt();
    	for(int i=0;i<7,source==0;i++)
    		if(degree[i]){
    			source=i;
    			break;
    		}
    	dfs(source);
    	for(int i=1;i<=n;i++)
    		if(!vis[i]) Halt();
    	memset(vis,false,sizeof(vis));
    	Output(source);
    	return 0;
    }
    

  • 相关阅读:
    空间谱专题16:间距选取分析
    复数矩阵分解的拆解思路(矩阵求逆/特征值分解)
    〖Linux〗Linux高级编程
    〖Linux〗安装和使用virtualenv,方便多个Python版本中切换
    〖Linux〗bash和expect执行ssh命令行sshcmd.exp
    【Linux】解决Android Stadio报错:error in opening zip file
    〖Linux〗穿越城墙之后,直接连接国内网站的路由配置
    〖Linux〗使用纯命令行来操作VBOX(宿主机不需要X11 Server)
    〖Network〗一行命令创建 http-server
    〖Linux〗多个JDK版本之间快速切换
  • 原文地址:https://www.cnblogs.com/Delostik/p/2140458.html
Copyright © 2011-2022 走看看