• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj 2676 Sudoku


            終于A了
            140ms+

            說一下我的思路:

            接受數據后,把每個為0的位置記錄下來(tatus[]),并計算該位置可能的狀態數,然后排序,從狀態數最小的開始搜索。也就是僅搜索為0的位置。

            不過對于這組數據:
            9
            000000000
            000000000
            000000000
            000000000
            000000000
            000000000
            000000000
            000000000
            000000000
            我估計我程序半年都出不了結果

            check(x,y,p)函數檢查(x,y)位置是否可以放p這個值。
            cal()函數計算初始可能的狀態數。

            #include<iostream>
            #include<string.h>
            #include<algorithm>
            using namespace std;
            int sudo[10][10];

            struct type
            {
                   int x,y,count;
            }status[82]; 
            int num_status=0;

            bool check(int i, int j, int t)//judge if it is possible to put digit t at (i,j)
            {
                 for(int k=1; k<=9; k++)
                         if(sudo[i][k]==t)return false;
                 for(int k=1; k<=9; k++)
                         if(sudo[k][j]==t)return false;
                 int x=(i-1)/3*3+1 ,y=(j-1)/3*3+1;
                 for(int s=0; s<=2; s++)
                 for(int tt=0; tt<=2; tt++)
                         if(sudo[x+s][y+tt]==t)return false;
                 return true;            
            }

            int cal(int i, int j)//calculate the the number of  possible status of (i,j)
            {
                int cnt=0;
                for(int p=1; p<=9; p++)
                        if(check(i,j,p))cnt++;
                       
                return cnt;
            }


            bool cmp(const type &a, const type &b)
            {
                 return a.count<b.count;
            }

            void dfs(int cnt,bool &find)
            {
                 if(cnt>num_status)return ;
                 if(find){ return ;}
                 int x=status[cnt].x;
                 int y=status[cnt].y;
                
                 if(cnt==num_status)
                 {
                          for(int p=1; p<=9; p++)
                                  if(check(x,y,p))
                                  {
                                                  sudo[x][y]=p;
                                                  for(int i=1; i<=9; i++)
                                                  {
                                                          for(int j=1; j<=9; j++)
                                                                  cout<<sudo[i][j];
                                                          cout<<endl;
                                                  }
                                                  find=true;
                                                  return ;
                                  }
                                  return ;   
                 }
                 for(int p=1; p<=9; p++)
                 {
                         if(check(x,y,p))
                         {
                          sudo[x][y]=p;
                          dfs(cnt+1,find);
                         }
                        sudo[x][y]=0; //剛開始一直錯就因為沒置0,check函數當當前位置是0的時候才有效。
                 }
                    
            }

             

            int main()
            {
                int t;
                cin>>t;
                while(t--)
                {
                   memset(sudo,0,sizeof sudo);
                   char ch;
                   num_status=0;
                   for(int i=1; i<=9; i++)
                           for(int j=1; j<=9; j++)
                           {
                            cin>>ch;
                            sudo[i][j]=int(ch-'0');
                            }
                           
                   for(int i=1; i<=9; i++)
                   for(int j=1; j<=9; j++)
                              if(sudo[i][j]==0)
                              {
                                  num_status++;
                                  status[num_status].x=i; status[num_status].y=j;
                                  status[num_status].count=cal(i,j);
                              }
                  
                   sort(status+1,status+1+num_status,cmp);
                  // for(int i=1; i<=num_status ;i++)
                    //      cout<<' '<<status[i].x<<' '<<status[i].y<<' '<<status[i].count<<endl;

                  
                   bool find=false;
                   dfs(1,find);
                   if(num_status==0)
                   {
                                for(int i=1; i<=9; i++)
                                {
                                          for(int j=1; j<=9; j++)
                                               cout<<sudo[i][j];
                                          cout<<endl;
                                }   
                   }
                }
                system("pause");
                return 0;
            }


            posted on 2010-08-18 19:09 田兵 閱讀(329) 評論(2)  編輯 收藏 引用 所屬分類: POJ

            評論

            # re: poj 2676 wa 2010-08-18 20:32 邱焜

            閣下代碼風格這么好,一定很快就能ac的  回復  更多評論   

            # re: poj 2676 Sudoku 2010-08-19 11:33 田兵

            @邱焜
            拜你吉言,A了 呵呵~~  回復  更多評論   

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導航

            統計

            常用鏈接

            留言簿(2)

            隨筆分類(65)

            隨筆檔案(65)

            文章檔案(2)

            ACM

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            精品久久久久久国产免费了| 精品熟女少妇av免费久久| 久久免费香蕉视频| 97久久婷婷五月综合色d啪蜜芽| 亚洲AV无一区二区三区久久 | 国产麻豆精品久久一二三| www亚洲欲色成人久久精品| 久久久久久曰本AV免费免费| 久久精品国产免费| 久久久久99这里有精品10 | 精品一区二区久久| 久久久午夜精品福利内容| 狠狠狠色丁香婷婷综合久久俺| 亚洲?V乱码久久精品蜜桃| jizzjizz国产精品久久| 色天使久久综合网天天| 国产99久久久国产精品~~牛| 久久久久亚洲AV无码网站| 中文字幕精品久久| 国产午夜精品久久久久九九电影| 看久久久久久a级毛片| 久久伊人五月丁香狠狠色| 久久夜色撩人精品国产| 国产午夜精品久久久久九九电影| 久久精品国产一区二区三区日韩| 中文精品久久久久人妻不卡| 久久久高清免费视频| 欧洲性大片xxxxx久久久| 国内精品久久久久国产盗摄| 久久99国产精品99久久| 999久久久免费精品国产| 久久久久亚洲精品天堂| 日产精品久久久久久久性色| AV无码久久久久不卡蜜桃| 精品无码久久久久国产动漫3d| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 久久香蕉国产线看观看精品yw| 丁香五月综合久久激情| 成人a毛片久久免费播放| 亚洲国产成人久久精品动漫| 丁香五月综合久久激情|