Cqoi2013 new Sudoku

Source: Internet
Author: User

Portal

This question is also a very violent search ......

Because the uniqueness is empty at the beginning, and there are only a lot of size restrictions, there is no need to worry about where to start the search, and then determine whether it is legal after the brute force search.

The most disgusting question is reading. Now I learned a trick to determine which part of the grid is located and use Lim [g] [I] [J] to indicate the relationship between the two grids I and j in the G Palace, the processing is still quite complex (in the Code), so there is nothing to pay attention to later. It is all about brute-force search.

In the end, this burst search will take place on two other points. I can't think of any better optimization to activate O2 ......

Take a look at the code.

// luogu-judger-enable-o2#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>#include<ctime>#define rep(i,a,n) for(int i = a;i <= n;i++)#define per(i,n,a) for(int i = n;i >= a;i--)#define enter putchar(‘\n‘)using namespace std;typedef long long ll;const int M = 1005;const int mod = 1e9 + 7;int read(){   int ans = 0,op = 1;   char ch = getchar();   while(ch < ‘0‘ || ch > ‘9‘)   {      if(ch == ‘-‘) op = -1;      ch = getchar();   }   while(ch >= ‘0‘ && ch <= ‘9‘)   {      ans *= 10;      ans += ch - ‘0‘;      ch = getchar();   }   return ans * op;}int num[11][11],hcnt,lcnt,px,py;int lim[11][11][11],vish[11][11],visl[11][11],visg[11][11];char s;void print(){   rep(i,1,9)   {      rep(j,1,9) printf("%d ",num[i][j]);enter;   }}bool pd(int g,int pos,int k){   rep(j,1,9)   {      int dx = g / 3 * 3 + (j - 1) / 3 + 1,dy = g % 3 * 3 + (j - 1) % 3 + 1;      if(lim[g][pos][j] == 1 && num[dx][dy] && k < num[dx][dy]) return 0;      if(lim[g][pos][j] == 2 && num[dx][dy] && k > num[dx][dy]) return 0;   }   return 1;}void buildh(int i,int k){   rep(j,1,6)   {      int g = (i - 1) * 3 + ((j - 1) >> 1);      int pos = ((k - 1) >> 1) * 3 + ((j - 1) & 1) + 1;      cin >> s;      lim[g][pos][pos+1] = (s == ‘>‘) ? 1 : 2;      lim[g][pos+1][pos] = (s == ‘>‘) ? 2 : 1;   }}void buildl(int i,int k){   rep(j,1,9)   {      int g = (i - 1) * 3 + (j - 1) / 3;      int pos = ((k-1) >> 1) * 3 + (j - 1) % 3 + 1;      cin >> s;      lim[g][pos][pos+3] = (s == ‘v‘) ? 1 : 2;      lim[g][pos+3][pos] = (s == ‘v‘) ? 2 : 1;   }}void build(){   rep(i,1,3)      rep(k,1,5)      (k & 1) ? buildh(i,k) : buildl(i,k);}void dfs(int x,int y){   int g = ((x - 1) / 3) * 3 + (y - 1) / 3;   int pos = (x - 1) % 3 * 3 + (y - 1) % 3 + 1;   rep(k,1,9)   {      if(vish[x][k] || visl[y][k] || visg[g][k]) continue;      if(!pd(g,pos,k)) continue;      vish[x][k] = visl[y][k] = visg[g][k] = 1,num[x][y] = k;      //print();      if(x == 9 && y == 9) print(),exit(0);      else (y == 9) ? dfs(x+1,1) : dfs(x,y+1);      vish[x][k] = visl[y][k] = visg[g][k] = 0,num[x][y] = 0;   }}int main(){   build();   /*rep(i,1,9)   {      rep(j,1,9)      {     printf("%d ",lim[1][i][j]);      }      enter;   }*/   dfs(1,1);   return 0;}

 

Cqoi2013 new Sudoku

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.