遞推+思路構造·UVA 11261·Bishops

題目大意:

國際象棋的象可以攻擊到自己所在的主副對角線的所有位置,給定幾個象棋棋子,問多少點不會被攻擊到;

解題思路:

如果數據量夠小,我們可以暴力查找,但4e4的矩陣不可能支持查詢點,我在做這道題的時候,一開始就感覺應該有一個初始化的過程,就是處理出點所在的對角線位置,而且這一思路也是正確的,因爲這樣的話我們實際上枚舉所有的對角線是否有點出現過即可,這一複雜度僅爲O(2n);

但是,憑藉老夫的聰明才智推出座標和對角線位置的關係還是極爲極(不)爲(太)輕(容)松(易)的,推了一會發現這神奇的關係:

設點座標(x, y),矩陣邊長爲n,

那麼其所在主對角線爲n - y + x(主對角線標號從右上到左下)

                副對角線爲x + y - 1(副對角線標號爲左上到右下);

而副對角線的兩端點和所在主對角線的關係爲 :

左上矩陣:

        id(主) = n - id(副) + 1;

右下矩陣:

       id(主) = id(副) - n + 1;

而我們知道不管是上三角還是下三角之類的半矩陣,i+2 的格子數比i多2,這樣就有一個遞推關係.

最後我們雖然處理出了副對角線的格子覆蓋數,但是也不能直接加,因爲每條副對角線絕對有效的格子只有兩個端點,因爲當前副對角線的中間格子是無法通過主對角線來判斷是否覆蓋的(這個地方仔細想一下,我們確實沒做這樣的處理),,還是要判斷這一副對角線是否覆蓋的.

AC代碼:

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
//#define   maxd          1010
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define   mc(x, y)     memcpy(x, y, sizeof(x))
#define   ms(x,y)      memset(x,y,sizeof(x))
#define   rep(i,n)      for(int i=0;i<(n);i++)
#define   repf(i,a,b)   for(int i=(a);i<=(b);i++)
#define   PI           pair<int,int>
//#define   mapp            make_pair
#define   FI            first
#define   SE            second
#define   IT            iterator
#define   PB            push_back
#define   Times         10
typedef   long long     ll;
typedef   unsigned long long ull;
typedef   long double   ld;
typedef   pair<int,int > pce;
//#define N 100
const double eps = 1e-10;
const double  pi = acos(-1.0);
const  ll    mod = 1e9+7;
const  int   inf = 0x3f3f3f3f;
//const  ll    INF = (ll)1e18+300;
const int   maxd = 100000 + 10;
const int   maxx = 10100;

int posi[maxd];
int nega[maxd];
int dp[maxd];
int n, m;
void solve() {
    for (int i = 2; i <= n ;i++) {
        dp[i] = dp[i - 2];
        int pos = n - i + 1;
        if(!posi[pos]) {
            dp[i] ++;
        }
        if(!posi[2 * n - pos]) {
            dp[i] ++;
        }
    }
    for (int i = 2 * n - 2; i > n ;i--) {
        dp[i] = dp[i + 2];
        int pos = i - n + 1;
        if(!posi[pos]) {
            dp[i] ++;
        }
        if(!posi[2 * n - pos]) {
            dp[i] ++;
        }
    }

}
int main() {
    int t;
    cin >> t;
    int kase = 0;
    while(t --) {
        kase ++;
        ms(posi, 0);
        ms(nega, 0);
        ms(dp, 0);
        cin >> n >> m;
        for (int i = 1; i <= m; i++) {
            int a, b;
            cin >> a >> b;
            posi[n - b + a] = 1;
            nega[a + b - 1] = 1;
        }
        if(posi[n] == 0) {
            dp[1] = dp[2 * n - 1] = 1;
        }
        solve();
        int ans = 0;
        for (int i = 1; i < 2 * n; i++) {
            if(!nega[i]){
                ans += dp[i];
            }
        }
        cout << "Case #" << kase << ": " ;
        cout << ans << endl;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章