Codeforces Round #249 (Div. 2) D. Special Grid (递推乱搞)(好题)
来源:互联网 发布:织梦dedecms 编辑:程序博客网 时间:2024/06/05 21:12
You are given an n × m grid, some of its nodes are black, the others are white. Moreover, it's not an ordinary grid — each unit square of the grid has painted diagonals.
The figure below is an example of such grid of size 3 × 5. Four nodes of this grid are black, the other 11 nodes are white.
Your task is to count the number of such triangles on the given grid that:
- the corners match the white nodes, and the area is positive;
- all sides go along the grid lines (horizontal, vertical or diagonal);
- no side contains black nodes.
The first line contains two integers n and m (2 ≤ n, m ≤ 400). Each of the following n lines contain m characters (zeros and ones) — the description of the grid. If the j-th character in the i-th line equals zero, then the node on the i-th horizontal line and on the j-th vertical line is painted white. Otherwise, the node is painted black.
The horizontal lines are numbered starting from one from top to bottom, the vertical lines are numbered starting from one from left to right.
Print a single integer — the number of required triangles.
3 5100001001000001
20
2 20000
4
2 21111
0
The figure below shows red and blue triangles. They are the examples of the required triangles in the first sample. One of the invalid triangles is painted green. It is invalid because not all sides go along the grid lines.
大致题意:
400*400的黑白网格,
求出有多少个三角形满足:
1.三边必须是网格上的线
2.三边上均不存在黑点
思路:
显然复杂度要在n^3以内
这题比较开放,观察图形特点,然后去解决就好
我的思路是,从四个方向递推,维护底边的合法性,然后O(n)求出以(x,y)为底边中点的合法三角形个数
过程中要O(1)判出某条斜边上是否存在黑点,可以用前缀和解决,斜边必然是45度或者135度的,所以用sun[n][m][2],维护两个斜向的前缀和即可
//#pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#include <string>#include <vector>#include <cstdio>#include <ctime>#include <bitset>#include <algorithm>#define SZ(x) ((int)(x).size())#define ALL(v) (v).begin(), (v).end()#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)#define REP(i,n) for ( int i=1; i<=int(n); i++ )#define rep(i,n) for ( int i=0; i<int(n); i++ )using namespace std;typedef long long ll;#define X first#define Y secondtypedef pair<ll,ll> pii;template <class T>inline bool RD(T &ret) {char c; int sgn;if (c = getchar(), c == EOF) return 0;while (c != '-' && (c<'0' || c>'9')) c = getchar();sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');ret *= sgn;return 1;}template <class T>inline void PT(T x) {if (x < 0) {putchar('-');x = -x;}if (x > 9) pt(x / 10);putchar(x % 10 + '0');}const int inf = 0x3f3f3f3f;const int N = 400+100;char mp[N][N];int sum[N][N][2];int n,m;inline bool ok(int x,int y,int a,int b){ if( x > a ) swap(x,a),swap(y,b); if( x < a && y < b) return ( sum[a][b][0]-sum[x-1][y-1][0] == 0 ); return (sum[x][y][1]-sum[a+1][b-1][1] == 0);}inline bool judge(int x,int y){ return ( x >= 1 && x <= n && y >= 1 && y <= m && mp[x][y] == '0');}int main(){ cin>>n>>m; REP(i,n) scanf("%s",mp[i]+1); REP(i,n) REP(j,m) sum[i][j][0] = sum[i-1][j-1][0]+( mp[i][j] == '1' ); rep(i,n) REP(j,m) sum[n-i][j][1] = sum[n-i+1][j-1][1]+( mp[n-i][j] == '1' ); int ans = 0; REP(x,n) REP(y,m){ if( mp[x][y] == '1' ) continue; bool ok1 = 1, ok2 = 1; REP(d,inf){ if( !ok1 && !ok2 ) break; if( ok1 ) ok1 = judge(x,y-d) && judge(x-d,y); if( ok2 ) ok2 = judge(x,y+d) && judge(x-d,y); ans += ok1 && ok(x,y-d,x-d,y); ans += ok2 && ok(x,y+d,x-d,y); } ok1 = ok2 = 1; REP(d,inf){ if( !ok1 && !ok2 ) break; if( ok1 ) ok1 = judge(x,y-d) && judge(x+d,y); if( ok2 ) ok2 = judge(x,y+d) && judge(x+d,y); ans += ok1 && ok(x,y-d,x+d,y); ans += ok2 && ok(x,y+d,x+d,y); } } REP(x,n) REP(y,m){ if( mp[x][y] == '1' ) continue; bool flag = 1; REP(d,inf){ if( !flag ) break; flag = judge(x,y-d) && judge(x,y+d) && (x-d >= 1); ans += flag && ok(x,y-d,x-d,y) && ok(x,y+d,x-d,y); } flag = 1; REP(d,inf){ if( !flag ) break; flag = judge(x,y-d) && judge(x,y+d) && (x+d <= n); ans += flag && ok(x,y-d,x+d,y) && ok(x,y+d,x+d,y); } flag = 1; REP(d,inf) { if( !flag ) break; flag = judge(x-d,y) && judge(x+d,y) && (y-d >=1); ans += flag && ok(x-d,y,x,y-d) && ok(x+d,y,x,y-d); } flag = 1; REP(d,inf){ if( !flag ) break; flag = judge(x-d,y) && judge(x+d,y) && (y+d <= m); ans += flag && ok(x-d,y,x,y+d) && ok(x+d,y,x,y+d); } } printf("%d\n",ans);}
- Codeforces Round #249 (Div. 2) D. Special Grid (递推乱搞)(好题)
- Codeforces Round #249 (Div. 2) D. Special Grid
- Codeforces Round #432(div.2) D题 暴力乱搞
- Codeforces Round #376 (Div. 2) D. 80-th Level Archeology(乱搞)
- Codeforces Round #271 (Div. 2) D. Flowers (递推 预处理)
- Codeforces Round #239 (Div. 2) D Long Path(递推)
- Codeforces Round #332 (Div. 2)-D Spongebob and Squares(枚举+递推)
- Codeforces Round #378 (Div. 2) D. Kostya the Sculptor(乱搞)
- Codeforces Round #240 (Div. 2)(D:dp递推)
- Codeforces Round #271 (Div. 2) D 递推
- Codeforces Round #277.5(Div. 2) F. Special Matrices【思维+Dp】好题~好题~
- Codeforces Round #256 (Div. 2) B. Suffix Structures (乱搞)
- Codeforces Round #377 (Div. 2) C. Sanatorium(规律乱搞)
- Codeforces Round #376 (Div. 2)F. Video Cards(乱搞)
- Codeforces Round #320 (Div. 2)D. "Or" Game(好题)
- Codeforces Round #426 (Div. 2) D. The Bakery(DP+线段树) 好题
- Codeforces Round #395 (Div. 2)D(想法题,好题)
- Codeforces Round #383 (Div. 2)D(dp,好题)
- windows平台select使用
- mysql 的官网在哪下载源码包呀
- hdu4638 Group(离线线段树)
- poj 1170 Shopping Offers
- Windows Sockets 网络编程——第三章 TCP/IP协议服务
- Codeforces Round #249 (Div. 2) D. Special Grid (递推乱搞)(好题)
- Java线程(5)Callable和Future
- iOS的三种弹框
- C++指针数组和指向指针的指针
- dos启动mysql时发生系统错误5拒绝访问的问题
- JavaWeb 的监听器
- 为什么/usr/local/apache2/bin/apxs明明存在,安装module的时候总是提示请指定有效的apxs路径呢?
- Java线程(6)锁对象Lock-同步问题更完美的处理方式
- 网络编程系列之十一 radius客户端(802.1x客户端)