hdu 6253 (bfs打表)

來源:互聯網
上載者:User

標籤:std   bsp   思路   資料   pac   def   long   個數   sig   

連結:http://acm.hdu.edu.cn/showproblem.php?pid=6253

題意:

馬可以往一個方向走兩步,然後轉個彎走一步,這樣算一次動作,求問馬n次動作後,能到達多少個點,重複到達的點只算一次。

思路:

一開始完全沒思路,畫圖找了半天把自己畫崩了,後面看到資料和範例感覺這應該是一道公式題,然後打了一個表。。

打表代碼:

#include<bits/stdc++.h>using namespace std;#define ull unsigned long longstruct node{    int x,y,step;};int dx[] = {2,2,-2,-2,1,1,-1,-1};int dy[] = {1,-1,1,-1,2,-2,2,-2};int ans[10000],vis[1000][1000];void bfs(){    node now;    queue<node>q;    now.x = 500,now.y = 500,now.step = 0;    ans[0] = 1;    vis[500][500] = 1;    q.push(now);    while(!q.empty()){        node now = q.front();        q.pop();        if(now.step == 20) continue;        node nex;        for(int i = 0;i < 8;i ++){            nex.x = now.x + dx[i];            nex.y = now.y + dy[i];            if(vis[nex.x][nex.y]==0)                nex.step = now.step+1,q.push(nex),ans[nex.step]++,vis[nex.x][nex.y]=1;        }    }    for(int i = 0;i <= 20;i ++)        cout<<ans[i]<<" ";//ans[i+1] += ans[i];    cout<<endl;}int main(){    bfs();    return 0;}

這個表求得是每一步多增加的點數,可以得到以下資料

1 8 32 68 96 120 148 176 204 232 260 288 316 344 372 400 428 456 484 512 540

我們可以發現這張表從120開始後面每一次都是+28

從120開始這個序列就可以變成一個等差數列,但是題目是要我們求所有的,那就直接等差數列前n項和公式就好了,把第一項設為120或者120後面隨意一個數字就好了,前n項求和後加上第一項前面的那些數的和就好了

這道題會爆long long ,我們用 unsigned long long 就好了

實現代碼;

#include<bits/stdc++.h>using namespace std;#define ull unsigned long long/*struct node{    int x,y,step;};int dx[] = {2,2,-2,-2,1,1,-1,-1};int dy[] = {1,-1,1,-1,2,-2,2,-2};int ans[10000],vis[1000][1000];void bfs(){    node now;    queue<node>q;    now.x = 500,now.y = 500,now.step = 0;    ans[0] = 1;    vis[500][500] = 1;    q.push(now);    while(!q.empty()){        node now = q.front();        q.pop();        if(now.step == 20) continue;        node nex;        for(int i = 0;i < 8;i ++){            nex.x = now.x + dx[i];            nex.y = now.y + dy[i];            if(vis[nex.x][nex.y]==0)                nex.step = now.step+1,q.push(nex),ans[nex.step]++,vis[nex.x][nex.y]=1;        }    }    for(int i = 0;i <= 20;i ++)        cout<<ans[i]<<" ";//ans[i+1] += ans[i];    cout<<endl;}int main(){    bfs();    return 0;}*/ //1 8 32 68 96 120 148 176 204 232 260 288 316 344 372 400 428 456 484 512 540//1 9 41 109 205 325 473 649 853 1085 1345 1633 1949 2293 2665 3065 3493 3949 4433 4945 5485int a[10] = {1, 9 ,41 ,109 ,205 ,325 ,473};int main(){    ull t,n,cas = 1;    ull ans;    cin>>t;    while(t--){        cin>>n;        if(n < 6) cout<<"Case #"<<cas++<<": "<<a[n]<<endl;        else{            ans = 148*(n-5) + (n-6)*(n-5)*14;            cout<<"Case #"<<cas++<<": "<<ans+325<<endl;        }    }    return 0;}

 

hdu 6253 (bfs打表)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.