题意:
给一些图片和矩阵, 要求出和矩阵匹配的图片。
思路:
机智的队友过的题。。。
首先,很明显大部分图片的黑白8联通块个数都是不同的,所以可以根据这个判断出大部分的图片。
不过很良心的样例给出了两个黑白联通块相同的情况, 这个时候求出两个图片的黑白比例,然后看比较靠近哪一个就取哪一个。。。
总之很神。。。
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
using namespace std;
#define N ( 1000 + 10 )
#define M ( 400000 + 10 )
#define LL long long
#define inf 0x3f3f3f3f
#define lson id << 1, l, m
#define rson id << 1 | 1, m + 1, r
#define mod 1000
int a[N][N];
int cx, cy;
int n;
void build( int x, int k ) {
while( x-- ) {
a[cx][cy] = k;
cy++;
if( cy == n ) ++cx, cy = 0;
}
}
bool out ( int x, int y ) {
if( x < 0 || x >= n || y < 0 || y >= n ) return 1;
return 0;
}
int dx[] = { -1, 1, 0, 0, 1, 1, -1, -1 };
int dy[] = { 0, 0, -1, 1, -1, 1, 1, -1 };
bool vis[N][N];
int num[2][1002000];
int wcnt, bcnt;
void dfs ( int x, int y, int k ) {
vis[x][y] = 1;
if( k == 0 ) {
++num[0][bcnt];
}
else ++num[1][wcnt];
for( int i = 0; i < 8; ++i ) {
int nx = x + dx[i];
int ny = y + dy[i];
if( out( nx, ny ) || vis[nx][ny] || a[nx][ny] != k ) continue;
dfs( nx, ny, k );
}
}
void bfs(int x, int y, int k) {
queue<int> q;
q.push(x), q.push(y);
vis[x][y] = 1;
while(!q.empty()) {
int ix = q.front(); q.pop();
int iy = q.front(); q.pop();
if(k == 0) ++num[0][bcnt];
else ++num[1][wcnt];
for(int i = 0; i < 8; ++i) {
int nx = ix + dx[i];
int ny = iy + dy[i];
if(out(nx, ny) || vis[nx][ny] || a[nx][ny] != k) continue;
vis[nx][ny] = 1;
q.push(nx), q.push(ny);
}
}
}
void print ( ) {
if( bcnt == 9 ) {
puts("Baekhyun");
}
if( bcnt == 5 ) {
if( wcnt == 1 ) {
puts("Chanyeol");
}
if( wcnt == 8 ) {
puts("Luhan");
}
if( wcnt == 2 ) {
double w = num[1][1] * 1.0 / num[1][2];
if( w > 12.5 ) {
puts("Xiumin");
}
else puts("Sehun");
}
}
if( bcnt == 1 ) {
if( wcnt == 3 ) {
puts("Chen");
}
if( wcnt == 2 ) {
puts("D.O");
}
}
if( bcnt == 2 ) {
if( wcnt == 13 ) {
puts("Kai");
}
if( wcnt == 8 ) {
puts("Suho");
}
if( wcnt == 4 ) {
puts("Tao");
}
}
if( bcnt == 3 ) {
puts("Kris");
}
if( bcnt == 6 ) {
puts("Lay");
}
}
int main () {
int T;
//freopen( "tt.txt", "r", stdin );
scanf("%d", &T );
while( T-- ) {
int m;
int x;
scanf("%d%d", &n, &m );
cx = 0, cy = 0;
int k = 1;
while( m-- ) {
int x;
scanf("%d", &x );
build( x , k);
k ^= 1;
}
wcnt = 0, bcnt = 0;
memset( vis, 0, sizeof( vis ) );
memset( num, 0, sizeof( num ) );
for( int i = 0; i < n; ++i ) {
for( int j = 0; j < n; ++j ) {
if( !vis[i][j] ) {
if( a[i][j] == 1 ) {
++wcnt;
bfs(i, j, 1);
}
else {
++bcnt;
bfs(i, j, 0);
}
}
}
}
print();
}
}
本文介绍了一种通过计算图片中黑白像素的联通块数量和比例来识别特定人物图像的方法。该算法能够有效地区分不同图像,并给出具体的实现代码示例。

3万+

被折叠的 条评论
为什么被折叠?



