• <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+

            說一下我的思路:

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

            不過對(duì)于這組數(shù)據(jù):
            9
            000000000
            000000000
            000000000
            000000000
            000000000
            000000000
            000000000
            000000000
            000000000
            我估計(jì)我程序半年都出不了結(jié)果

            check(x,y,p)函數(shù)檢查(x,y)位置是否可以放p這個(gè)值。
            cal()函數(shù)計(jì)算初始可能的狀態(tài)數(shù)。

            #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; //剛開始一直錯(cuò)就因?yàn)闆]置0,check函數(shù)當(dāng)當(dāng)前位置是0的時(shí)候才有效。
                 }
                    
            }

             

            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 田兵 閱讀(333) 評(píng)論(2)  編輯 收藏 引用 所屬分類: POJ

            評(píng)論

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

            閣下代碼風(fēng)格這么好,一定很快就能ac的  回復(fù)  更多評(píng)論   

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

            @邱焜
            拜你吉言,A了 呵呵~~  回復(fù)  更多評(píng)論   

            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆分類(65)

            隨筆檔案(65)

            文章檔案(2)

            ACM

            搜索

            積分與排名

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            99国产欧美久久久精品蜜芽 | 久久受www免费人成_看片中文| 99久久精品久久久久久清纯| 丰满少妇人妻久久久久久4| 国产精品综合久久第一页 | 亚洲精品国产美女久久久| 99999久久久久久亚洲| 久久伊人中文无码| 久久66热人妻偷产精品9| 午夜精品久久久久久| 久久精品国产99国产精品澳门| 久久久无码精品亚洲日韩蜜臀浪潮| 久久无码人妻一区二区三区午夜| 久久久久亚洲AV综合波多野结衣| 久久发布国产伦子伦精品| 久久精品国产一区二区| 久久精品中文闷骚内射| 亚洲七七久久精品中文国产| 日本三级久久网| 精品久久久噜噜噜久久久| 亚洲色欲久久久久综合网| 岛国搬运www久久| 99久久免费国产精品| 亚洲AV乱码久久精品蜜桃| 亚洲精品乱码久久久久久不卡| 精品国产婷婷久久久| 香港aa三级久久三级| 久久香蕉国产线看观看乱码| 亚洲级αV无码毛片久久精品| 国产99久久久国产精品小说| 欧美与黑人午夜性猛交久久久| 久久香蕉国产线看观看99| 久久se精品一区二区| 久久精品国产亚洲av日韩| 久久久亚洲欧洲日产国码二区| 亚洲AV日韩精品久久久久久| 性欧美丰满熟妇XXXX性久久久| 亚洲国产精品无码久久久秋霞2 | 午夜人妻久久久久久久久| 伊人久久无码中文字幕| 亚洲AV无码久久精品蜜桃|