您的位置:首页 > 运维架构

递推+思路构造·UVA 11261·Bishops

2018-03-28 17:37 369 查看
题目大意:
国际象棋的象可以攻击到自己所在的主副对角线的所有位置,给定几个象棋棋子,问多少点不会被攻击到;
解题思路:
如果数据量够小,我们可以暴力查找,但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
== 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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: