• <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>
            付翔的專欄
            在鄙視中成長 記錄成長的點滴
            posts - 106,  comments - 32,  trackbacks - 0

            原始博客地址: http://www.fuxiang90.com/2012/07/usaco1-5-checker-challenge/拿到題目我的第一反應是八皇后問題,順利的寫出了遞歸解法,弄完這個,感覺自己寫遞歸和回溯有了一定的進步了,至此第一章做完了,再接再厲。

            但是提交后,在13 這個測試樣例超時,然后就在想怎么剪枝

            • 之前在判斷放棋子是否沖突的時候,是在放的位置往四個方向拓展,如果沒有沖突就放 。現在改進為直接判斷 和之前放置的棋子是否沖突。
            • 對稱剪枝,這個在百度之后才知道的 ,這個是關鍵,直接砍掉一般的時間
            還有說是用位運算,這個不熟,下次去學一下。
            /*
            ID:fuxiang2
            PROG: checker
            LANG: C++
            */
            #include 
            <iostream>
            #include 
            <fstream>
            #include 
            <stack>
            #include 
            <string>
            #include 
            <vector>
            #include 
            <queue>
            #include 
            <map>
            #include 
            <list>
            #include 
            <algorithm>
            #include 
            <set>
            #include 
            <cmath>
            #include 
            <cstring>
            #include 
            <cstdlib>
             
            #define REP(i, n) for (int i=0;i<int(n);++i)
            #define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
            #define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
            #define REP_1(i, n) for (int i=1;i<=int(n);++i)
            #define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
            #define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
            #define EACH(it, A) for (typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
             
            using namespace std;
            ofstream fout (
            "checker.out");
            ifstream fin (
            "checker.in");
             
            const int N = 14;
            int graph[N][N];
            int n;
            int ans ;
            int result ;
            // 類似八皇后問題
            int used[N];
            //list <int >path;
            int path[N];
             
            bool isok(int x,int y)
            {
                
            if(x >=1 && x<= n && y >= 1 && y <= n)
                    
            return true;
                
            return false;
            }
            int dir[4][2= { {-1,-1} ,{-1,1},{1,1},{1,-1} };
            bool check(int x,int y )
            {
                
            int nx = x;
                
            int ny = y;
                
            int n = x -1;
                
            if(n == 0)
                    
            return true;
             
                FOR_1(i,
            1,n){
                    nx 
            = i;
                    ny 
            = path[i];
                    
            if( abs(x-nx) == abs(y-ny))
                        
            return false;
                }
                
            return true;
             
                
            //FOR_1(i,0,3){
                
            //    nx = x +  dir[i][0];
                
            //    ny = y +  dir[i][1];
                
            //    while(isok(nx,ny) ){
                
            //        if(graph[nx][ny] == 1)
                
            //            return false;
                
            //        nx += dir[i][0];
                
            //        ny += dir[i][1];
                
            //    }
                
            //}
                
            //return true;
             
            }
             
            void place(int col,int row)
            {
                graph[row][col] 
            = 1;
                
            if(row== n){
                    ans 
            ++;
                    
            if(result + ans <= 3){
                        
            //list<int >::iterator iter = path.begin();
                        
            //fout<< *iter;
                        fout<<path[1];
                        
            //for(iter ++ ; iter != path.end() ; iter ++)
                        for(int i = 2 ; i <= n ; i ++)
                            fout 
            <<" "<< path[i];
                        fout
            <<endl;
                    }
                    graph[row][col] 
            = 0;
                    
            return ;
                }
                FOR_1(i,
            1,n){
                    
            if(used[i] == 0 && check(row+1,i ) == true )
                    {
                        path[row
            +1= i;//path.push_back(i);
                        used[i] = 1;
                        place(i,row
            +1);
                        
            //path.pop_back();
                        used[i] = 0;
                    }
                }
                graph[row][col] 
            = 0;
             
            }
            void work(int n)
            {
                result 
            = 0;
             
                FOR_1(j,
            1,n/2) {// 列
                    path[1= j;//path.push_back(j);
                    used[j]  = 1;
                    place(j,
            1);
                    
            //path.pop_back();
                    used[j] = 0;
                }
                
            int re =  ans;
                result 
            = ans;
                
            if(re <3 || n%2 == 1){
                    
            int t = n/2 + 1;
                    ans 
            = 0;
                    path[
            1= t;//path.push_back(j);
                    used[t]  = 1;
                    place(t,
            1);
             
                }
                
            if( n% 2 == 1)
                    result 
            += re + ans;
                
            else
                    result 
            += re;
            }
             
            int main()
            {
                fin
            >>n;
                work(n);
                fout
            << result<<endl;
                
            return 0;
             
            }

            原始博客地址: http://www.fuxiang90.com/2012/07/usaco1-5-checker-challenge/
            posted on 2012-07-10 10:41 付翔 閱讀(215) 評論(0)  編輯 收藏 引用

            <2011年7月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            CSDN - 我的blog地址

            博客

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            免费精品99久久国产综合精品| 一级A毛片免费观看久久精品| 囯产精品久久久久久久久蜜桃| 久久无码AV一区二区三区| 老男人久久青草av高清| 九九久久99综合一区二区| 日产久久强奸免费的看| 久久精品国产清高在天天线| 日本福利片国产午夜久久| 久久婷婷国产剧情内射白浆| 国产一区二区三区久久精品| 久久亚洲中文字幕精品一区| 色综合久久最新中文字幕| 香蕉久久av一区二区三区| 久久国产成人午夜aⅴ影院| 欧美一区二区三区久久综| 无码8090精品久久一区 | 欧美国产成人久久精品| 久久久久国产精品熟女影院 | 国产欧美久久久精品影院| 日本三级久久网| 国产精品免费看久久久| 97精品依人久久久大香线蕉97 | 久久无码精品一区二区三区| 99久久婷婷免费国产综合精品| 国产香蕉久久精品综合网| 久久精品视频91| 久久97久久97精品免视看| 国产欧美一区二区久久| 色婷婷综合久久久久中文一区二区 | 狠狠精品久久久无码中文字幕 | 亚洲AV伊人久久青青草原| 久久久久国产亚洲AV麻豆| 国产69精品久久久久9999| 久久99国产精品久久| 久久久综合九色合综国产| 久久狠狠色狠狠色综合| 狠狠久久综合| 亚洲精品97久久中文字幕无码| 无码人妻少妇久久中文字幕| 人妻丰满?V无码久久不卡|