UVALive 4763

一开始,没敢写,感觉会超时。。。其实就是暴力搜索。DFS

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<queue>
  7 #define clc(a,b) memset(a,b,sizeof(a))
  8 #define N 100
  9 #define eps 1e-6
 10 #include<stack>
 11 #include<deque>
 12 #include<vector>
 13 using namespace std;
 14 const int  MAXD = 1e5+10;
 15 const int  MAXM = 51;
 16 const int  INF = 0x3f3f3f3f;
 17 typedef long long ll;
 18
 19 char G[10][10];
 20 int  row[10][10],coloum[10][10],cr[10][10],row1[26];
 21 int t;
 22 int sum;
 23 void dfs(int x,int y)
 24 {
 25     if(x==9)
 26     {
 27         sum++;
 28         return;
 29     }
 30     if(G[x][y]>=‘1‘&&G[x][y]<=‘9‘)
 31     {
 32         if(y==8)
 33             dfs(x+1,0);
 34         else
 35             dfs(x,y+1);
 36         return ;
 37     }
 38     else if(G[x][y]==‘e‘)
 39     {
 40         int flag=0;
 41         for(int i=2; i<10; i=i+2)
 42         {
 43             if(!row[x][i]&&!coloum[y][i]&&!cr[(x/3)*3+y/3+1][i])
 44             {
 45                 flag=1;
 46                 G[x][y]=i+‘0‘;
 47                 row[x][i]=1;
 48                 coloum[y][i]=1;
 49                 cr[(x/3)*3+y/3+1][i]=1;
 50                 if(y==8)
 51                     dfs(x+1,0);
 52                 else
 53                     dfs(x,y+1);
 54                 G[x][y]=‘e‘;
 55                 row[x][i]=0;
 56                 coloum[y][i]=0;
 57                 cr[(x/3)*3+y/3+1][i]=0;
 58             }
 59         }
 60         if(!flag)
 61             return;
 62     }
 63     else if(G[x][y]==‘o‘)
 64     {
 65         int flag=0;
 66         for(int i=1; i<=9; i=i+2)
 67         {
 68             if(!row[x][i]&&!coloum[y][i]&&!cr[(x/3)*3+y/3+1][i])
 69             {
 70                 flag=1;
 71                 G[x][y]=i+‘0‘;
 72                 row[x][i]=1;
 73                 coloum[y][i]=1;
 74                 cr[(x/3)*3+y/3+1][i]=1;
 75                 if(y==8)
 76                     dfs(x+1,0);
 77                 else
 78                     dfs(x,y+1);
 79                 G[x][y]=‘o‘;
 80                 row[x][i]=0;
 81                 coloum[y][i]=0;
 82                 cr[(x/3)*3+y/3+1][i]=0;
 83             }
 84         }
 85         if(!flag)
 86             return ;
 87     }
 88     else if(G[x][y]>=‘a‘&&G[x][y]<=‘z‘&&G[x][y]!=‘e‘&&G[x][y]!=‘o‘)
 89     {
 90         int num=G[x][y]-‘a‘;
 91         if(row1[num])
 92         {
 93             if(!row[x][row1[num]]&&!coloum[y][row1[num]]&&!cr[(x/3)*3+y/3+1][row1[num]])
 94             {
 95                 G[x][y]=row1[num]+‘0‘;
 96                 row[x][row1[num]]=1;
 97                 coloum[y][row1[num]]=1;
 98                 cr[(x/3)*3+y/3+1][row1[num]]=1;
 99                 if(y==8)
100                     dfs(x+1,0);
101                 else
102                     dfs(x,y+1);
103                 G[x][y]=num+‘a‘;
104                 row[x][row1[num]]=0;
105                 coloum[y][row1[num]]=0;
106                 cr[(x/3)*3+y/3+1][row1[num]]=0;
107             }
108             else
109                 return ;
110         }
111         else
112         {
113             int flag=0;
114             for(int i=1; i<=9; i++)
115             {
116                 if(row[x][i]==0&&coloum[y][i]==0&&cr[(x/3)*3+y/3+1][i]==0)
117                 {
118                     flag=1;
119                     row1[num]=i;
120                     G[x][y]=i+‘0‘;
121                     row[x][i]=1;
122                     coloum[y][i]=1;
123                     cr[(x/3)*3+y/3+1][i]=1;
124                     if(y==8)
125                         dfs(x+1,0);
126                     else
127                         dfs(x,y+1);
128                     G[x][y]=num+‘a‘;
129                     row1[num]=0;
130                     row[x][i]=0;
131                     coloum[y][i]=0;
132                     cr[(x/3)*3+y/3+1][i]=0;
133                 }
134             }
135             if(flag==0)
136                 return ;
137         }
138     }
139     else if(G[x][y]==‘0‘)
140     {
141         int flag=0;
142         for(int i=1; i<=9; i++)
143         {
144             if(!row[x][i]&&!coloum[y][i]&&cr[(x/3)*3+y/3+1][i]==0)
145             {
146                 flag=1;
147                 G[x][y]=i+‘0‘;
148                 row[x][i]=1;
149                 coloum[y][i]=1;
150                 cr[(x/3)*3+y/3+1][i]=1;
151                 if(y==8)
152                     dfs(x+1,0);
153                 else
154                     dfs(x,y+1);
155                 G[x][y]=‘0‘;
156                 row[x][i]=0;
157                 coloum[y][i]=0;
158                 cr[(x/3)*3+y/3+1][i]=0;
159             }
160         }
161         if(flag==0)
162             return;
163     }
164 }
165
166 int main()
167 {
168     //freopen("in.txt","r",stdin);
169     scanf("%d",&t);
170     while(t--)
171     {
172         sum=0;
173         clc(row,0);
174         clc(coloum,0);
175         clc(cr,0);
176         clc(row1,0);
177         for(int i=0;i<9;i++)
178         {
179             scanf("%s",G[i]);
180         }
181         for(int i=0; i<9; i++)
182         {
183             for(int j=0; j<9; j++)
184             {
185
186                 if(G[i][j]>=‘1‘&&G[i][j]<=‘9‘)
187                 {
188                     row[i][G[i][j]-‘0‘]=1;
189                     coloum[j][G[i][j]-‘0‘]=1;
190                     cr[(i/3)*3+j/3+1][G[i][j]-‘0‘]=1;
191                 }
192             }
193         }
194         dfs(0,0);
195         printf("%d\n",sum);
196     }
197     return 0;
198 }

时间: 2024-10-04 09:02:20

UVALive 4763的相关文章

2015暑假训练赛团体赛(8.3)

    ID Origin Title     12 / 37 Problem A UVALive 4763 Sudoku Extension   23 / 80 Problem B UVALive 4764 Bing it       Problem C UVALive 4765 String of Candied Haws     4 / 19 Problem D UVALive 4766 Gold Mines       Problem E UVALive 4767 Machine Tra

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

UVALive 7077 Little Zu Chongzhi&#39;s Triangles (有序序列和三角形的关系)

这个题……我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了……结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小,想到了回溯每个棍的分组,最多分5组,结果发现超时了……最大是5^12 =  244,140,625,厉害呢…… 后来想贪心,首先想暴力出所有可能的组合,结果发现替换问题是一个难题……最后T T ,我就断片了.. 等看了别人的办法以后,我才发现我忽视了三角形的特性,和把数据排序以后的特点. 如果数据从

Gym 100299C &amp;&amp; UVaLive 6582 Magical GCD (暴力+数论)

题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点时,我们对gcd相同的只保留一个,那就是左端点最小的那个,只有这样才能保证是最大,然后删掉没用的. UVaLive上的数据有问题,比赛时怎么也交不过,后来去别的oj交就过了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&qu

HDU 4763:Theme Section(KMP)

http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Problem Description It's time for music! A lot of popular musicians are invited to join us in the music festival. Each of them will play one of their representative songs. To make the progr

UVALive 6511 Term Project

Term Project Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 651164-bit integer IO format: %lld      Java class name: Main 解题:强连通分量 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1

UVALive 6508 Permutation Graphs

Permutation Graphs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 650864-bit integer IO format: %lld      Java class name: Main 解题:逆序数 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long l

UVALive 2659+HUST 1017+ZOJ 3209 (DLX

UVALive 2659 题目:16*16的数独.试了一发大白模板. /* * @author: Cwind */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include