【組隊賽#5】BNU 4299 God Save the i-th Queen (數組映射)

來源:互聯網
上載者:User

標籤:acm   比賽題   

【題目連結】click here~~

【題目大意】給你一個N*M的棋盤,並且給定T個已經固定位置的皇后(橫座標,縱座標已知),求下一個合法放置皇后的可能數

【解題思路】

因為只是求下一個皇后的位置,而不是求所有其他可能,因此可以按照如下思路進行:

 對n*n的矩陣進行映射,將其轉換為1*(n+m)的單行模式。
    1、行列可以直接映射。
    2、對於對角線有兩種情況(畫圖驗證)
    1.1.映射為x+y模式(一三區間)
    2..1映射為x-y+Y模式(二四區間)  (注意!前面的x是映射的行,Y後才是列)

代碼

/*   Author:HRW    對n*n的矩陣進行映射,將其轉換為1*(n+m)的單行模式。    1、行列可以直接映射。    2、對於對角線有兩種情況(畫圖驗證)         1.映射為x+y模式(一三區間)         2.映射為x-y+Y模式(二四區間)  (注意!前面的x是映射的行,Y後才是列)*/#include <bits/stdc++.h>using namespace std;typedef long long LL;const int maxn = 30000 + 10;int usedx[maxn], usedy[maxn], usedl[maxn+maxn],usedr[maxn+maxn];//int shuru()//{//    int sum=0;//    char ch;//    while((ch=getchar())<'0'||ch>'9'); sum=ch-'0';//    while((ch=getchar())>='0'&&ch<='9') sum=sum*10+ch-'0';//    return sum;//}int main(){    int X,Y,N;    while(cin >> X >> Y >> N,X)    {        memset(usedl,0,sizeof(usedl));        memset(usedr,0,sizeof(usedr));        memset(usedx,0,sizeof(usedx));        memset(usedy,0,sizeof(usedy));        for(int i = 0; i < N; ++i){            int x,y;            cin>>x>>y;            ++usedx[x];    //映射行            ++usedy[y];  //映射列            ++usedl[Y+x-y]; //2,4區間            ++usedr[x+y]; //1,3區間        }        LL ans = 0;        for(int i = 1; i <= X; ++i){            for(int j = 1; j <= Y; ++j){                if(!usedx[i] && !usedy[j] && !usedl[i-j+Y] && !usedr[i+j])  ans++;            }        }        cout << ans << endl;    }    return 0;}


【組隊賽#5】BNU 4299 God Save the i-th Queen (數組映射)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.