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

            The Fourth Dimension Space

            枯葉北風寒,忽然年以殘,念往昔,語默心酸。二十光陰無一物,韶光賤,寐難安; 不畏形影單,道途阻且慢,哪曲折,如渡飛湍。斬浪劈波酬壯志,同把酒,共言歡! -如夢令

            #

            張宏數據結構第二課——動態線性鏈表類(sqlist測試版) 歡迎大家測試

                 摘要:   #include <iostream>#include <cmath>#include<algorithm>#include<cstring>using namespace std;struct node{    int data;&n...  閱讀全文

            posted @ 2009-02-23 22:47 abilitytao 閱讀(1189) | 評論 (6)編輯 收藏

            POJ 2560-Freckles 最小生成樹

            今天碰到一個最小生成樹(數據結構基礎算法)
            鞏固了一下 沒有什么大的收獲。

            不過發現原來在程序后面加個system("pause")也能AC;
            代碼如下:
            #include <iostream>
            #include
            <algorithm>
            #include
            <cmath>
            using namespace std;
            #define  MAX 101
            #define INFINITE 1000000000

            struct node
            {
                
            double a;
                
            double b;
            }
            dot[MAX];

            double value[MAX][MAX];
            bool visit[MAX];
            double dis[MAX];
            int n;

            double distance(int i,int j)
            {
                
            double temp=0;
                temp
            =sqrt((dot[i].a-dot[j].a)*(dot[i].a-dot[j].a)+(dot[i].b-dot[j].b)*(dot[i].b-dot[j].b));
                
            return temp;
            }


            double  prim()
            {
                
            double sum=0;
                
            int i,j;
                
            int k;
                memset(visit,
            false,sizeof(visit));
                
            for(i=1;i<=n;i++)
                
            {

                    dis[i]
            =value[1][i];
                }

                visit[
            1]=true;
                
            int mark;
                
            double test=10000000000;
                sum
            =0;
                
            for(j=1;j<=n-1;j++)
                
            {
                    test
            =INFINITE;
                    
            for(i=1;i<=n;i++)
                    
            {
                        
            if(visit[i]==false&&dis[i]<test)
                        
            {

                            test
            =dis[i];
                            mark
            =i;
                        }

                        
                    }

                    sum
            +=test;visit[mark]=true;
                    
            for(i=1;i<=n;i++)
                    
            {

                        
            if(visit[i]==false&&value[mark][i]<dis[i])
                            dis[i]
            =value[mark][i];

                    }

                }

                
            return sum;
            }








                
            int main ()
                
            {
                    
            int i,j;
                    scanf(
            "%d",&n);
                    
            for(i=1;i<=n;i++)
                    
            {

                        scanf(
            "%lf%lf",&dot[i].a,&dot[i].b);
                    }

                    
            for(i=1;i<=n;i++)
                    
            {
                        
            for(j=1;j<=n;j++)
                        
            {

                            value[i][j]
            =distance(i,j);
                        }


                    }

                    printf(
            "%.2f\n",prim());
                    system(
            "pause");
                    
            return 0;
            }


            posted @ 2009-02-22 20:30 abilitytao 閱讀(1566) | 評論 (2)編輯 收藏

            POJ 2392-Space Elevator 背包問題(多重背包)

            最近好像跟背包問題有緣啊,已經做了好幾道了。不過感覺這個問題確有它值得研究之處,也從中學到不少東西呵&

            這道題目的大意是:有n種材料,每種材料有數量c和長度h還有一個特殊限制(可以看成一個背包的容量)
            出題者要你求出用這n種材料可以累加出的最大長度而且其中的每一種材料中的任何一個元素都不能高過這個限制a.想了想,貌似可以用多重背包來形容這個問題,當然我是非專業的,說得不恰當還請大家原諒;

            下面的這段代碼中有幾點值得注意:
            首先是這道題中的關鍵部分和我寫的1276題非常相似(參考了網路上大牛的代碼 先謝過~)
            通過這兩個題我發現:這種方法只適用于(這里我們用背包問題的原始定義來形象的說明)物品的重量等于其價值的情況;
            非這種情況那就請老老實實按書上的方法做吧(如果說錯了 還望您指出,不過我是這樣認為的);
            這種方法確實很快,而且比課本上提到的dp方法更牛,一定要掌握呵&

            其次是這道題一定要先排序,再dp;
            為什么呢?我想了想 給出下面一個例子:
            如果數據是這樣的:
            2
            7 40 3
            5 23 1
            如果不排序 那么按照那個算法26是不可及的
            但是如果排序(按a從小到大)
            變成
            5 23 1
            7 40 3
            那么26就可能了
            是不是很神奇?但這就是區別 所以本題必須要排序;
            我后來又想了想 發現這其實是一個策略的問題,在實際中,我們也當然會把安全的部分放在底層,這總方法感覺上就更穩妥些,而且也帶有某種貪心的性質我覺得。
            如果哲學的來理解,我引用一句經典的話:九層之臺,起于累土,千里之行,始于足下。呵呵,我感覺還挺符合本題的,不知道您覺得呢?

            AC CODE:
            #include <iostream>
            #include
            <cmath>
            #include
            <algorithm>
            #include
            <cstdio>
            using namespace std;

            struct node
            {

                
            int h;
                
            int a;
                
            int c;
            }
            a[401];

            int cmp(node a,node b)
            {
                
            return a.a<b.a;
            }



            bool dp[40001];


            int main ()
            {

                
            int n;
                
            int i,j,k;
                scanf(
            "%d",&n);

                
            for(i=1;i<=n;i++)
                
            {
                    scanf(
            "%d%d%d",&a[i].h,&a[i].a,&a[i].c);
                }

                sort(a
            +1,a+1+n,cmp);
                
                
                
            int max=0;
                dp[
            0]=true;
                
            for(i=1;i<=n;i++)
                
            {
                    
            for(k=max;k>=0;k--)
                        
            if(dp[k]==true)
                        
            {
                                
            for(j=1;j<=a[i].c;j++)
                            
            {

                                
            int temp;
                                temp
            =k+j*a[i].h;
                                
            if(temp>a[i].a)
                                    
            break;
                                dp[temp]
            =true;
                                
            if(temp>max)
                                    max
            =temp;



                            }

                        }

                    


                }

                    printf(
            "%d\n",max);
                    
            return 0;


                



            }








            posted @ 2009-02-22 01:03 abilitytao 閱讀(2169) | 評論 (6)編輯 收藏

            POJ 1276-Cash Machine 特殊情況下的背包問題+強剪枝

                 摘要: 今天又做了一個背包的題目,有點郁悶~如果單純只考算法的話應該是很容易的,可是由于數據范圍太大,一直都過不了。。。汗~TLE了2個小時,自己嘗試了N種剪枝方法但還是過不去。最后無奈只好到網絡上搜索了一下,借用了網上大牛代碼中的一個剪枝方法,才過掉這道題的。。。剛開始的時候我是這么寫的,沒有任何剪枝,結果當然是TLE啦: #include <iostream>#include&...  閱讀全文

            posted @ 2009-02-20 17:46 abilitytao 閱讀(2556) | 評論 (0)編輯 收藏

            POJ 1837-Balance 0/1背包問題的擴展

            //copyright by abilitytao
            #include <iostream>
            using namespace std;


            int pos[21];
            int weight[21];
            int mid=4000;
            int result[21][8000];
            int c,g;


            int main()
            {

                
            int i,j,k;
                scanf(
            "%d%d",&c,&g);
                
            for(i=1;i<=c;i++)
                    scanf(
            "%d",&pos[i]);
                
            for(i=1;i<=g;i++)
                    scanf(
            "%d",&weight[i]);
                
            for(i=1;i<=c;i++)
                
            {

                    result[
            1][pos[i]*weight[1]+4000]++;
                }

                
            // 以上是dp初始化,接下來將用動態規劃不斷地掛上新的砝碼;

                
            for(i=2;i<=g;i++)
                
            {

                    
            for(j=1;j<=c;j++)
                        
            for(k=1;k<=8000;k++)
                        
            {

                            
            if(result[i-1][k]!=0)
                            result[i][k
            +weight[i]*pos[j]]+=result[i-1][k];
                        }

                }

                printf(
            "%d\n",result[g][4000]);

                
            return 0;
            }





            這個題目做的確實有點郁悶,說實話,我是參考網上的代碼做的,即使如此,我還是沒法證明那個狀態轉移方程的正確性;
            只是感覺上像是對的,汗~后來聯想到弗洛伊德和bellman-ford我這樣說了:一切盡在迭代中啊~
            如果有牛人能幫我證明一下的話 我感激不盡呵& 我的qq:64076241

            再說說做這個題目的收獲,那個+4000的小技巧倒是很受用,呵呵;
            還有就是對動態規劃的感覺越來越好了,有的時候能憑直覺把題做出來了...

            posted @ 2009-02-20 00:03 abilitytao 閱讀(1652) | 評論 (0)編輯 收藏

            POJ 1546 Basically Speaking-數進制轉換

            原題鏈接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1546

            題目的意思很簡單,給你一個n進制的數,讓你把它轉換成m進制,但是有一個限制,如果輸出的數字大于7為的話無法在計算器上顯示,所以要輸出ERROR;

            說說做這道題的體會吧,方法很easy,不過感覺代碼寫的長了點,第一個函數里用了map容器,后來發現其實并沒有減少工作量,所以第二的函數里就干脆沒使用了 O(∩_∩)O~
            所犯的幾個錯誤是 轉換成desbase進制是,while循環里應該是num!=0,剛開始的時候寫成了num/desbase!=0;呵呵
            其次是發現不能在全局狀態下往容器里面添加元素。害我總是編譯不了,查了半天才知道的。。。

            下面是我的代碼:
            #include <iostream>
            #include
            <cstring>
            #include
            <algorithm>
            #include
            <cmath>
            #include
            <map>
            using namespace std;


            map
            <char,int>amap;
            char temp[100];
            int oribase;
            int desbase;



            int convet(char a[],int oribase)
            {
                
            int len=strlen(a);
                
            int i;
                
            int result=0;
                
            for(i=0;i<len;i++)
                
            {
                    result
            +=amap[a[len-1-i]]*pow((double)oribase,i);
                }

                
            return result;
            }


            void convet2(int num,int desbase)
            {
                
            char test[100];
                
            int i=0;
                
            int testnum;
                
            int len;
                
            while(num!=0)
                
            {
                    testnum
            =num%desbase;
                    num
            =num/desbase;
                    
            if(testnum==15)
                        test[i]
            ='F';
                    
            else    if(testnum==14)
                        test[i]
            ='E';
                    
            else if(testnum==13)
                        test[i]
            ='D';
                    
            else if(testnum==12)
                        test[i]
            ='C';
                    
            else if(testnum==11)
                        test[i]
            ='B';
                    
            else if(testnum==10)
                        test[i]
            ='A';
                    
            else if(testnum==9)
                        test[i]
            ='9';
                    
            else if(testnum==8)
                        test[i]
            ='8';
                    
            else if(testnum==7)
                        test[i]
            ='7';
                    
            else if(testnum==6)
                        test[i]
            ='6';
                    
            else if(testnum==5)
                        test[i]
            ='5';
                    
            else if(testnum==4)
                        test[i]
            ='4';
                    
            else if(testnum==3)
                        test[i]
            ='3';
                    
            else if(testnum==2)
                        test[i]
            ='2';
                    
            else if(testnum==1)
                        test[i]
            ='1';
                    
            else if(testnum==0)
                        test[i]
            ='0';
                    i
            ++;
                }

                test[i]
            ='\0';
                reverse(test,test
            +strlen(test));
                strcpy(temp,test);
            }


            int main ()
            {
                amap[
            '0']=0;amap['1']=1;amap['2']=2;amap['3']=3;
                amap[
            '4']=4;amap['5']=5;amap['6']=6;amap['7']=7;
                amap[
            '8']=8;amap['9']=9;amap['A']=10;amap['B']=11;
                amap[
            'C']=12;amap['D']=13;amap['E']=14;amap['F']=15;
                
            int midresult;
                
            int len;
                
            while(scanf("%s%d%d",temp,&oribase,&desbase)!=EOF)
                
            {
                    
                    midresult
            =convet(temp,oribase);
                    convet2(midresult,desbase);
                    len
            =strlen(temp);
                    
            if(len>7)
                    
            {

                        printf(
            "  ERROR\n");

                    }

                    
            else
                        printf(
            "%7s\n",temp);
                }

                
            return 0;


            }

            posted @ 2009-02-19 23:50 abilitytao 閱讀(1052) | 評論 (0)編輯 收藏

            POJ 3624-Charm Bracelet 簡單的0/1背包問題

            經典的背包問題.

            #include <iostream>
            #include 
            <algorithm>
            using namespace std;

            int w[3500];
            int v[3500];
            int dp[13000];


            int main ()
            {

                
            int n,m;
                
            int i,j;
                scanf(
            "%d%d",&n,&m);
                
            for(i=1;i<=n;i++)
                {

                    scanf(
            "%d%d",&w[i],&v[i]);

                }
                memset(dp,
            0,sizeof(dp));
                
            for(i=1;i<=n;i++)
                {

                    
            for(j=m;j>=w[i];j--)
                    {

                        
            if(dp[j]<dp[j-w[i]]+v[i])
                            dp[j]
            =dp[j-w[i]]+v[i];
                    }
                }
                printf(
            "%d\n",dp[m]);
                
            return 0;


            }

            posted @ 2009-02-19 21:16 abilitytao 閱讀(2004) | 評論 (3)編輯 收藏

            POJ 2033 Alphacode -動態規劃

            這道題的題意大概是這樣的:給出一個字符串,如果將字母表中的26個字母依次映射成數字1-26,這樣便形成一個密碼,a cipher.學過密碼學就知道,這是一個簡單的替代密碼不過似乎并不是那么好用,雖然加密的確很方便,可是解密就麻煩了,因為存在很多種解密的方法,所以本題就是要你求出一串數字究竟有多少種解密方法。

             

            個人心得:這是我寒假做的最后幾道題目的其中一道了,一拿到這道題目我就立刻想到了動態規劃,呵呵,看來偶對dp開始有點感覺了;

            解本題的關鍵在于將dp[i]裝換成為dp[i-1]和dp[i-2]的關系

            首先確定dp[1]和dp[2]的值 前者肯定等于1 而后者可能為2也可能為1 這個應該很容易判斷出來

            然后在判斷i>=3的情況

            如果ch[i]為字符‘0’ 那么dp[i]肯定等于dp[i-2].因為一個字符不可能被映射成0,它必須和前一個字符成為一個整體

            如果不是字符零,那么看看這個字符與前一個字符是不是能構成一個小于等于26且大于0的數字

            并且這個數字的第一位不是0,那么dp[i]=dp[i-1]+dp[i-2];

            如果不是讓面的兩種情況 dp[i]=dp[i-1];

             

            這個題目其實很容易想到,不過剛做的時候沒有設出temp變量,只是規定兩個數字的范圍

            '2'>=ch[i-1]>'0'&&ch[i]<='6'這樣19就被和諧掉了所以一直調都不對最后才想到的 O(∩_∩)O~


            Source Code

             

            Problem: 2033  User: abilitytao

            Memory: 272K  Time: 16MS

            Language: C++  Result: Accepted


            #include<iostream>

            using namespace std;

             

             

            long dp[50001];

            char ch[50001];

             

             

            int work(char a[])

            {

                
            int temp;

                
            int len;

                len
            =strlen(a);

                
            int i;

                dp[
            0]=1;

                temp
            =(ch[0]-'0')*10+(ch[1]-'0');

                
            if(temp>0&&temp<=26&&ch[1]!='0')

                       dp[
            1]=2;

                
            else

                       dp[
            1]=1;

                
            for(i=2;i<len;i++)

                {

                       temp
            =(ch[i-1]-'0')*10+(ch[i]-'0');

                       
            if(ch[i]=='0')

                              dp[i]
            =dp[i-2];

                      

                       
            else if(temp>0&&temp<=26&&ch[i-1]!='0')

                              dp[i]
            =dp[i-1]+dp[i-2];

                       
            else

                              dp[i]
            =dp[i-1];

                }

                
            return dp[len-1];

            }

             

            int main ()

            {

                
            while(cin>>ch)

                {

                       
            if(ch[0]=='0')

                              
            break;

                       cout
            <<work(ch)<<endl;

                }

                
            return 0;

            }


            posted @ 2009-02-19 13:09 abilitytao 閱讀(1674) | 評論 (0)編輯 收藏

            Pku 3620 Avoid the lake -dfs基本問題

            Pku 3620解題報告

            一、原題

            Description

            Farmer John's farm was flooded in the most recent storm, a fact only aggravated by the information that his cows are deathly afraid of water. His insurance agency will only repay him, however, an amount depending on the size of the largest "lake" on his farm.

            The farm is represented as a rectangular grid with N (1 ≤ N ≤ 100) rows and M (1 ≤ M ≤ 100) columns. Each cell in the grid is either dry or submerged, and exactly K (1 ≤ KN × M) of the cells are submerged. As one would expect, a lake has a central cell to which other cells connect by sharing a long edge (not a corner). Any cell that shares a long edge with the central cell or shares a long edge with any connected cell becomes a connected cell and is part of the lake.

            Input

            * Line 1: Three space-separated integers: N, M, and K
            * Lines 2..K+1: Line i+1 describes one submerged location with two space separated integers that are its row and column: R and C

            Output

            * Line 1: The number of cells that the largest lake contains. 

            Sample Input

            3 4 5 
            3 2 
            2 2 
            3 1 
            2 3 
            1 1 

            Sample Output

            4 
            二.題意闡述 
            1.其實本題可以轉化成如下的純數學模型。 
            2.給出n*m大小的矩陣,初始值全為0 
            3.在特定的位置將a[i][j]置成1 
            4.求取相連的1個數的最大值。 
              
            .算法 
              此題看似簡單,但實際上蘊藏玄機。懂得此法的同學將無疑大大提升自己的能力,再將其拓展,則能夠解決一大類問題,此題包含著一個及其重要的數學模型----遞歸搜索(名字是自己取的,不專業請原諒)。 
              只要設計一個函數,給一個入口參數(I,j),使得運行該函數后,得到與(I,j)方塊相連的方塊數。 
            然后在用循環,將每一個方塊都找一遍,求出最大值即可。 
              那個函數,這是本題的精髓,用遞歸的方法,可以讓其自動朝著四周的方向搜索滿足條件的方塊,直到求出與某一格相連的小方塊數目的總數。 
              其實,我一直想把上回的超級瑪麗做出來,這個題給了我基本的搜索本領。我想,只要我繼續研究下去就一定能解決超級瑪麗的問題了 o(_)o… 

             

            四、解題過程

            本來想用循環的方法來做的,可是發現用循環貌似無法結束查找。于是我請教了陳澤怡學長,他提示我用遞歸的方法來解此題,這才使我恍然大悟。用遞歸可以不斷地調用函數體本身,直到結束查找!~

            .程序代碼

             

            #include<iostream>

            #include
            <math.h>

            #include
            <algorithm>

            using namespace std;

            int a[101][101]={0};

            int num;

            void search(int i,int j)

            {

                  

                   
            if(a[i][j]==1)

                   {

                          a[i][j]
            =-1;

                          num
            ++;

                          search(i,j
            +1);

                          search(i
            +1,j);

                          search(i,j
            -1);

                          search(i
            -1,j);

                   }

                  

            }
            //定義遞歸搜索函數,入口參數為所要調查的小方塊位置

                  

                  

                  

            int main()

            {

                         

                   
            int n,m,k,i,j,x,y,max=0;

                   cin
            >>n>>m>>k;

                   
            for(i=1;i<=k;i++)

                   {

                          cin
            >>x>>y;

                          a[x][y]
            =1;//把要調查的位置置為1;

                   }

                   
            for(i=1;i<=n;i++)

                          
            for(j=1;j<=m;j++)

                          {

                                 num
            =0;

                                 search(i,j);

                                 
            if(num>=max)

                                        max
            =num;

                  

                          }
            //用循環的方法將整個矩陣都查找一遍,并求出與某小方塊相連方塊數的最大值;

                          cout
            <<max<<endl;//輸出該最大值;

                          
            return 0;

                                

            }

             

            六.小結 
            這道題的代碼看上去很短,但是確非常非常的重要,當然 ,這并不代表我完全掌握了這類方法,如果說不是深度或者廣度優先怎么辦,如何用隊列?這是接下來我需要面對和解決的問題另外,我寫報告的水平有待提高,如果不是知道我的意圖,會有人看得懂這份報告么?ACM講究的是團隊作戰,即使我很優秀 ,也不過是個獨斷獨行的人,這是不能成功的,要想取得成績,先得學會與同學交流。 
            呵呵 這是我的第一篇結題報告 值得紀念啊 當然還要特別感謝賈瓊學姐哦 O(∩_∩)O~ 

            posted @ 2009-02-19 13:07 abilitytao 閱讀(1040) | 評論 (1)編輯 收藏

            Reverse Polish Notation——標程

                 摘要:   //Copyright by abilitytao//All right not reserved!!  #include <process.h>#include <iostream.h>#include <conio.h>#include&nbs...  閱讀全文

            posted @ 2009-02-19 13:05 abilitytao 閱讀(774) | 評論 (0)編輯 收藏

            僅列出標題
            共42頁: First 34 35 36 37 38 39 40 41 42 
            性做久久久久久久久浪潮| 久久久久久亚洲精品影院| 久久丫忘忧草产品| 久久综合给合综合久久| 日本久久久精品中文字幕| 久久99精品国产自在现线小黄鸭 | 久久久久亚洲av无码专区喷水| 久久久久久A亚洲欧洲AV冫| 狠狠色婷婷综合天天久久丁香 | 久久久久亚洲av成人网人人软件| 国产呻吟久久久久久久92| 91精品日韩人妻无码久久不卡| 国产精品久久久久影院嫩草| 97久久精品国产精品青草| 国产精品久久久久…| 久久精品免费观看| 丰满少妇人妻久久久久久4| 国产福利电影一区二区三区久久久久成人精品综合 | 亚洲日本va中文字幕久久| 亚洲国产另类久久久精品黑人 | 9191精品国产免费久久| 国产精品gz久久久| 久久久久亚洲精品天堂久久久久久 | 久久精品无码一区二区无码| 亚洲中文字幕无码久久综合网| 亚洲国产精品无码久久一区二区| 久久婷婷五月综合97色| 久久99毛片免费观看不卡| 久久国产美女免费观看精品 | 久久精品aⅴ无码中文字字幕不卡| 国产精品久久久久久久人人看| 亚洲AV无码成人网站久久精品大| 久久久一本精品99久久精品88| 久久青草国产手机看片福利盒子 | 欧美日韩精品久久免费| 久久这里只有精品18| 伊人丁香狠狠色综合久久| 亚洲精品NV久久久久久久久久| 午夜欧美精品久久久久久久| 欧美日韩中文字幕久久伊人| 日韩美女18网站久久精品 |