LA 5140 Squares

来源:互联网 发布:淘宝分享有礼 编辑:程序博客网 时间:2024/04/16 15:08

题意:给你一个n乘n的点阵(2 <= n <= 9), 连接其中一些相邻点的边,水平或者竖直。求出各个size的正方形的个数。

方法:Brute force

因为n比较小,直接检查就好了。检查的方式有很多种,这里用到的是先枚举正方形左上的点(i,j),再从小到大枚举边长k,检查当前左上端点和边长的组合是否合法的方法就是找出对应的左下端点b和右上端点a,check一下边(b,b上方的点)和边(a,a左边的点)存不存在。如果合法,那就记录下来,即

                work[i*n+j][k] = true;
再枚举正方形右下的点,从小到大枚举边长k,用类似上面的方法检查当前右下端点和边长的组合是否合法,如果合法的话,再看一下对应的左上端点c是否有work[b][k]。如果同样可行,那就对相应长度的正方形数量加一。

突然发现Live Archive可以刷榜,就用ios::sync_with_stdio(false); cin.tie(0);来提高速度,分分钟超过马孟起:)。

Code:

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <string>#include <vector>#include <stack>#include <bitset>#include <cstdlib>#include <cmath>#include <set>#include <list>#include <deque>#include <map>#include <queue>#include <fstream>#include <cassert>#include <cmath>#include <sstream>#include <time.h>#include <complex>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))#define DFOR(a,b,c) for (int (a)=(b);(a)>=(c);--(a))#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))#define FOREACH(a,b) for (auto &(a) : (b))#define rep(i,n) FOR(i,0,n)#define repn(i,n) FORN(i,1,n)#define drep(i,n) DFOR(i,n-1,0)#define drepn(i,n) DFOR(i,n,1)#define MAX(a,b) a = Max(a,b)#define MIN(a,b) a = Min(a,b)#define SQR(x) ((LL)(x) * (x))#define Reset(a,b) memset(a,b,sizeof(a))#define fi first#define se second#define mp make_pair#define pb push_back#define all(v) v.begin(),v.end()#define ALLA(arr,sz) arr,arr+sz#define SIZE(v) (int)v.size()#define SORT(v) sort(all(v))#define REVERSE(v) reverse(ALL(v))#define SORTA(arr,sz) sort(ALLA(arr,sz))#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))#define PERMUTE next_permutation#define TC(t) while(t--)#define forever for(;;)#define PINF 1000000000000#define newline '\n'#define test if(1)coutusing namespace std;using namespace std;typedef vector<int> vi;typedef vector<vi> vvi;typedef pair<int,int> ii;typedef pair<double,double> dd;typedef pair<char,char> cc;typedef vector<ii> vii;typedef long long ll;typedef unsigned long long ull;typedef pair<ll, ll> l4;const double pi = acos(-1.0);const int maxn = 81;bool adj[maxn][maxn];bool work[maxn][10];int ans[10];int n, m;void solve();int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int kase = 0;    bool first = true;    while (cin >> n >> m)    {        if (first)  first = false;        else cout << "\n**********************************\n\n";        ++kase;        cout << "Problem #" << kase << "\n\n";        solve();    }}void solve(){    ios::sync_with_stdio(false);    cin.tie(0);    Reset(adj, false);    char dir;   int r, c;    rep(i, m)    {        cin >> dir >> r >> c;        --r, --c;        int id = r*n+c;        if (dir == 'H')        {            adj[id][id+1] = true;            adj[id+1][id] = true;        }        else        {            id = c*n+r;            adj[id][id+n] = true;            adj[id+n][id] = true;        }    }    Reset(ans, 0);    Reset(work, 0);    for (int i = 0; i < n-1; ++i)        for (int j = 0; j < n-1; ++j)        {            for (int k = 1; k < n-i && k < n-j; ++k)            {                int a = i*n+j+k; //top right corner                int b = (i+k)*n+j; // bottom left corner                if (!adj[a][a-1] || !adj[b][b-n])   break;                work[i*n+j][k] = true;            }        }    for (int i = n-1; i > 0; --i)        for (int j = n-1; j > 0; --j)        {            for (int k = 1; j-k >= 0 && i-k >= 0; ++k)            {                int a = i*n+j-k; // bottom left corner                int b = (i-k)*n+j; // top right corner                if (!adj[a][a+1] || !adj[b][b+n]) break;                ans[k] += work[(i-k)*n+j-k][k];            }        }    bool hasAns = false;    for (int i = 1; i <= 9; ++i)    {        if (!ans[i])    continue;        hasAns = true;        cout << ans[i] << " square (s) of size " << i << newline;    }    if (!hasAns)        cout << "No completed squares can be found.\n";    }/* 4 16  H 1 1 H 1 3 H 2 1 H 2 2 H 2 3 H 3 2 H 4 2 H 4 3 V 1 1 V 2 1 V 2 2 V 2 3 V 3 2 V 4 1 V 4 2 V 4 3  2 3  H 1 1 H 2 1 V 2 1 */



0 0
原创粉丝点击