• <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 付翔 閱讀(220) 評論(0)  編輯 收藏 引用

            <2011年2月>
            303112345
            6789101112
            13141516171819
            20212223242526
            272812345
            6789101112

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            CSDN - 我的blog地址

            博客

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            91麻豆精品国产91久久久久久| 国产精品久久一区二区三区 | 国产一级做a爰片久久毛片| 久久夜色精品国产欧美乱| 99精品久久精品一区二区| 久久久久免费精品国产| 中文字幕乱码久久午夜| 久久免费线看线看| 久久久噜噜噜久久| 国产韩国精品一区二区三区久久| 久久亚洲高清观看| 欧美精品九九99久久在观看| 91视频国产91久久久| 久久亚洲视频| 伊人丁香狠狠色综合久久| 久久精品国产男包| 久久av高潮av无码av喷吹| AV狠狠色丁香婷婷综合久久| 国内精品综合久久久40p| 国产一区二区精品久久岳| 亚洲AV无码久久精品蜜桃| 久久艹国产| 久久精品国产亚洲AV无码麻豆| 三级三级久久三级久久| 亚洲欧洲久久久精品| 日韩AV毛片精品久久久| 久久强奷乱码老熟女| 精品久久一区二区| 精品久久久久久无码专区不卡| 久久久久久久免费视频| 久久一区二区三区免费| 久久精品国产99久久香蕉| 久久九九久精品国产| 欧美精品一区二区精品久久| 99久久精品国产麻豆| 久久综合九色综合欧美狠狠| 国产高潮国产高潮久久久| 久久不见久久见免费视频7| 久久久久久九九99精品| 国内精品久久久久久99| 久久国产精品-久久精品|