全 WA 求助
642144
This_Rrhar楼主2023/10/22 15:01

rt,使用的是第一篇题解的方法,而且已经用几篇题解的代码对拍,输出完全一致。

代码:

#include<bits/stdc++.h>
using namespace std;

#define H 70
#define W 9005
#define siz(x) (int(x.size()))
#define db(x) double(x)

const double pi=3.14159265358;
const int lim=75;

template<typename T>void Max(T&a,T b){a=max(a,b);}
template<typename T>void Min(T&a,T b){a=min(a,b);}

double Rad(double ang){return ang/180.0*pi;};

const char*word="0123456789()+-*/";

struct IMG
{
	vector<vector<bool>>img;
	int n,m;
	IMG()=default;
	IMG(int x,int y)
	{
		n=x,m=y,img.resize(n);
		fill(img.begin(),img.end(),vector<bool>(m));
	}
	IMG(const char*s)
	{
		stringstream ss(s);
		string t;
		n=m=0,img.clear();
		while(ss>>t)
		{
			vector<bool>v;
			for(const char c:t)v.push_back(c=='#');
			img.push_back(v),n++,Max(m,siz(v));
		}
	}
	IMG(const string&s)
	{
		stringstream ss(s);
		string t;
		n=m=0,img.clear();
		while(ss>>t)
		{
			vector<bool>v;
			for(const char c:t)v.push_back(c=='#');
			img.push_back(v),n++,Max(m,siz(v));
		}
	}
};

struct point
{
	int x,y;
	point()=default;
	point(int x,int y):x(x),y(y){}
};

struct dpoint
{
	double x,y;
	dpoint()=default;
	dpoint(double x,double y):x(x),y(y){}
	dpoint(int x,int y):x(db(x)),y(db(y)){}
};

bool inIMG(point p,point edge){return 0<=p.x&&p.x<edge.x&&0<=p.y&&p.y<edge.y;}

IMG wordset[]=
{
R"(
...........########...........
.........############.........
.......################.......
......##################......
.....####################.....
....######################....
...########################...
...##########....##########...
..#########........#########..
..########..........########..
.#########..........#########.
.########............########.
.########............########.
.########............########.
########..............########
########..............########
########..............########
########..............########
########..............########
########..............########
########..............########
########..............########
########..............########
########..............########
########..............########
.########............########.
.########............########.
.########............########.
.#########..........#########.
..########..........########..
..#########........#########..
...##########....##########...
...########################...
....######################....
.....####################.....
......##################......
.......################.......
.........############.........
...........########...........
)",
R"(
.............####..........
...........#######.........
........##########.........
.....#############.........
..################.........
.#################.........
.#################.........
.#################.........
.#################.........
..######..########.........
..###.....########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
..........########.........
.##########################
###########################
###########################
###########################
###########################
.##########################
)",
R"(
.........#########..........
......##############........
...###################......
..#####################.....
.#######################....
.#######################....
.########################...
.#########......#########...
.#######.........#########..
.#######..........########..
.#######..........########..
.#######..........########..
.#######..........########..
..######..........########..
.................#########..
.................########...
................#########...
...............##########...
..............##########....
.............###########....
............###########.....
...........###########......
..........###########.......
.........###########........
........###########.........
.......###########..........
......###########.....#####.
.....###########.....#######
....###########......#######
...###########.......#######
..###########........#######
.###########.........#######
############################
############################
############################
############################
############################
.###########################
)",
R"(
.........##########.........
.....################.......
...####################.....
..######################....
..#######################...
..########################..
..########################..
..########.......##########.
..#######.........#########.
..#######..........########.
..#######..........########.
...######..........########.
...................########.
...................########.
..................########..
.................#########..
..........###############...
.........###############....
.........##############.....
.........###############....
.........################...
..........################..
.................##########.
...................########.
...................#########
....................########
....................########
....................########
....................########
...................#########
..###.............##########
.########........##########.
.##########################.
.#########################..
##########################..
.########################...
.######################.....
....#################.......
.......###########..........
)",
R"(
.................#####........
................#######.......
...............########.......
..............#########.......
.............##########.......
............###########.......
............###########.......
...........############.......
..........#############.......
.........##############.......
........###############.......
.......################.......
.......########.#######.......
......########..#######.......
.....########...#######.......
....########....#######.......
...#########....#######.......
...########.....#######.......
..########......#######.......
.########.......#######.......
##############################
##############################
##############################
##############################
##############################
##############################
...............########.......
...............########.......
...............########.......
...............########.......
...............########.......
...............########.......
.........####################.
........#####################.
........#####################.
........#####################.
........#####################.
.........####################.
)",
R"(
...######################...
...#######################..
...#######################..
...#######################..
...#######################..
...######################...
...#######..................
...#######..................
...#######..................
...#######..................
...#######..................
...#######..................
..########..########........
..####################......
..######################....
..#######################...
..########################..
..########################..
..#########################.
..########.......##########.
.....##...........#########.
...................#########
....................########
....................########
....................########
....................########
....................########
....................########
...#...............#########
..####............#########.
.########.......###########.
.##########################.
.#########################..
#########################...
.#######################....
..#####################.....
....#################.......
........##########..........
)",
R"(
....................######...
...............###########...
............###############..
..........#################..
........###################..
.......####################..
......####################...
.....################........
....############.............
...###########...............
...#########.................
..#########..................
..########...................
.########....................
.########....................
.#######....########.........
.#######..#############......
#########################....
##########################...
###########################..
###########################..
############......##########.
##########.........#########.
#########...........#########
########.............########
########.............########
########.............########
.#######.............########
.#######.............########
.########...........#########
.#########.........#########.
..##########.....###########.
...#########################.
...########################..
....######################...
.....####################....
......##################.....
........##############.......
...........########..........
)",
R"(
###########################.
############################
############################
############################
############################
###########################.
#######...........#########.
#######...........#########.
#######..........#########..
#######..........#########..
#######..........########...
#######.........#########...
#######.........########....
#######........#########....
.#####.........#########....
...............########.....
..............#########.....
..............########......
.............#########......
.............########.......
............#########.......
............#########.......
............########........
...........#########........
...........########.........
..........#########.........
..........########..........
..........########..........
.........########...........
.........########...........
........#########...........
........########............
........########............
.......########.............
.......########.............
.......#######..............
.......#######..............
.........#####..............
)",
R"(
..........#########.........
........#############.......
......#################.....
.....###################....
....#####################...
...#######################..
...#######################..
...#########.....#########..
..#########.......#########.
..########.........########.
..########.........########.
..########.........########.
..########.........########.
..########.........########.
...########.......########..
...#########.....#########..
....#####################...
.....###################....
......#################.....
......#################.....
....#####################...
...#######################..
..#########......##########.
.########..........########.
.########..........#########
########............########
########............########
########............########
########............########
#########..........#########
#########..........#########
.##########......##########.
.##########################.
..########################..
..########################..
...######################...
....####################....
......################......
.........##########.........
)",
R"(
.........#########..........
.......#############........
.....#################......
....###################.....
...#####################....
..#######################...
.########################...
.##########......#########..
.#########........#########.
#########..........########.
########............#######.
########............#######.
########............########
########............########
########............########
#########..........#########
.########.........##########
.##########......###########
..##########################
..##########################
...#########################
....########################
......############..########
........########....#######.
....................#######.
...................########.
...................########.
..................########..
.................#########..
...............##########...
.............############...
.........###############....
....###################.....
...###################......
...##################.......
...################.........
...##############...........
...############.............
....######..................
)",
R"(
...............####..
..............######.
............########.
...........##########
..........###########
.........############
........############.
.......###########...
......###########....
.....###########.....
.....##########......
....##########.......
....#########........
...#########.........
...########..........
..#########..........
..########...........
.#########...........
.########............
.########............
.########............
#########............
########.............
########.............
########.............
########.............
########.............
########.............
########.............
########.............
########.............
########.............
.########............
.########............
.########............
.#########...........
..########...........
..#########..........
..#########..........
...#########.........
...##########........
....#########........
.....#########.......
.....##########......
......###########....
.......###########...
........###########..
.........############
..........###########
...........##########
............#########
.............#######.
...............####..
.................#...
)",
R"(
..####...............
.######..............
#########............
##########...........
###########..........
############.........
.############........
...###########.......
....###########......
.....##########......
......##########.....
.......##########....
........#########....
.........#########...
..........########...
..........#########..
...........########..
...........#########.
............########.
............########.
............########.
............#########
.............########
.............########
.............########
.............########
.............########
.............########
.............########
.............########
.............########
.............########
............########.
............########.
............########.
...........#########.
...........########..
..........#########..
..........#########..
.........#########...
........##########...
.......##########....
.......#########.....
.....###########.....
....###########......
...###########.......
..###########........
############.........
###########..........
##########...........
#########............
.#######.............
..####...............
...#.................
)",
R"(
...........#####...........
..........#######..........
..........#######..........
..........#######..........
..........#######..........
..........#######..........
..........#######..........
..........#######..........
..........#######..........
..........#######..........
###########################
###########################
###########################
###########################
###########################
###########################
###########################
..........#######..........
..........#######..........
..........#######..........
..........#######..........
..........#######..........
..........#######..........
..........#######..........
..........#######..........
..........#######..........
..........#######..........
...........#####...........
)",
R"(
.##########################.
############################
############################
############################
############################
############################
.###########################
)",
R"(
...........####...........
..........######..........
..........#######.........
.........########.........
.........########.........
..........#######.........
..........######..........
.#####....######.....####.
.######...######...#######
#########..#####.#########
###############.##########
##########################
##########################
.########################.
......##############......
.........########.........
........###########.......
.......#############......
.....########.#######.....
....########..########....
....########..#########...
...########....########...
...########....########...
....#######.....#######...
.....#####......######....
......###.........##......
)",
R"(
......................####..
......................######
.....................#######
.....................#######
....................########
....................#######.
...................########.
...................#######..
..................########..
..................#######...
.................########...
.................#######....
................########....
................########....
................#######.....
...............########.....
...............#######......
..............########......
..............#######.......
.............########.......
.............#######........
............########........
............#######.........
...........########.........
...........#######..........
..........########..........
..........#######...........
..........#######...........
.........########...........
.........#######............
........########............
........#######.............
.......########.............
.......#######..............
......########..............
......#######...............
.....########...............
.....#######................
....########................
....#######.................
...########.................
...########.................
...#######..................
..########..................
..#######...................
.########...................
.#######....................
########....................
#######.....................
.######.....................
..#####.....................
)",
};

point wordpos[16]={{7,4},{7,6},{7,5},{7,5},{7,4},{8,5},{7,5},{8,6},{7,5},{7,5},{4,8},{4,9},{13,6},{23,5},{7,6},{3,5}};

IMG randset[16][lim];

IMG cover(const IMG&img1,const IMG&img2,point d)
{
	IMG res(img1);
	for(int i=max(0,-d.x);i<min(img2.n,img1.n-d.x);i++)
		for(int j=max(0,-d.y);j<min(img2.m,img1.m-d.y);j++)
			res.img[i+d.x][j+d.y]=img2.img[i][j];
	return res;
}

IMG denoise(const IMG&img)
{
	point d[]={{0,0},{1,0},{0,1},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
	IMG res(img);
	for(int i=0;i<img.n;i++)for(int j=0;j<img.m;j++)
	{
		int s1=0,s2=0;
		for(point k:d)
		{
			int nx=i+k.x,ny=j+k.y;
			if(inIMG(point(nx,ny),point(img.n,img.m)))s1+=img.img[nx][ny],s2++;
		}
		res.img[i][j]=(s1*2>s2);
	}
	return res;
}

vector<IMG>split(IMG img,int k=15)
{
	point mn(INT_MAX,INT_MAX),mx(INT_MIN,INT_MIN);
	queue<point>pts;
	vector<IMG>res;
	
	auto bfs=[&](point st)
	{
		point d[]={{1,0},{0,1},{-1,0},{0,-1}};
		queue<point>q;
		pts.push(st),q.push(st);
		Min(mn.x,st.x),Min(mn.y,st.y),Max(mx.x,st.x),Max(mx.y,st.y);
		img.img[st.x][st.y]=false;
		while(!q.empty())
		{
			point p=q.front();
			q.pop();
			for(point i:d)
			{
				int nx=p.x+i.x,ny=p.y+i.y;
				if(inIMG(point(nx,ny),point(img.n,img.m))&&img.img[nx][ny])
					pts.push(point(nx,ny)),q.push(point(nx,ny)),img.img[nx][ny]=false,
					Min(mn.x,nx),Min(mn.y,ny),Max(mx.x,nx),Max(mx.y,ny);
			}
		}
	};
	
	for(int j=0;j<img.m;j++)for(int i=0;i<img.n;i++)if(img.img[i][j])
	{
		mn=point(INT_MAX,INT_MAX),mx=point(INT_MIN,INT_MIN);
		bfs(point(i,j));
		if(siz(pts)>=k)
		{
			IMG tmp(mx.x-mn.x+1,mx.y-mn.y+1);
			while(!pts.empty())
			{
				point p=pts.front();
				pts.pop(),tmp.img[p.x-mn.x][p.y-mn.y]=true;
			}
			res.push_back(tmp);
		}
		while(!pts.empty())pts.pop();
	}
	
	return res;
}

IMG resize(const IMG&img,point e)
{
	IMG res(e.x,e.y);
	double kx=db(e.x)/img.n,ky=db(e.y)/img.m;
	if(kx<1)
	{
		for(int i=0;i<img.n;i++)
		{
			if(ky<1)
			{
				for(int j=0;j<img.m;j++)if(img.img[i][j])res.img[int(i*kx)][int(j*ky)]=true;
			}
			else
			{
				for(int j=0;j<e.y;j++)if(img.img[i][int(j/ky)])res.img[int(i*kx)][j]=true;
			}
		}
	}
	else
	{
		for(int i=0;i<e.x;i++)
		{
			if(ky<1)
			{
				for(int j=0;j<img.m;j++)if(img.img[int(i/kx)][j])res.img[i][int(j*ky)]=true;
			}
			else
			{
				for(int j=0;j<e.y;j++)if(img.img[int(i/kx)][int(j/ky)])res.img[i][j]=true;
			}
		}
	}
	return res;
}

IMG rotate(const IMG&img,double angle)
{
	angle=Rad(angle);
	dpoint mn(1e5,1e5),mx(-1e5,-1e5);
	vector<dpoint>pts;
	
	auto rtt=[](dpoint p,double angle)
	{
		dpoint t(p.y,-p.x);
		dpoint nxt(t.x*cos(angle)-t.y*sin(angle),t.x*sin(angle)+t.y*cos(angle));
		return dpoint(-nxt.y,nxt.x);
	};
	
	for(int i=0;i<img.n;i++)for(int j=0;j<img.m;j++)if(img.img[i][j])pts.push_back(dpoint(i,j));
	for(dpoint&i:pts)
	{
		i=rtt(i,angle);
		Min(mn.x,i.x),Min(mn.y,i.y),Max(mx.x,i.x),Max(mx.y,i.y);
	}
	IMG res(int(mx.x-mn.x+1),int(mx.y-mn.y+1));
	for(dpoint&i:pts)res.img[int(i.x-mn.x)][int(i.y-mn.y)]=true;
	return res;
}

IMG balance(const IMG&img)
{
	IMG res(65,38);
	point sum(0,0);
	int c=0;
	for(int i=0;i<img.n;i++)for(int j=0;j<img.m;j++)if(img.img[i][j])sum.x+=i,sum.y+=j,c++;
	point m((sum.x*2+c)/(c*2),(sum.y*2+c)/(c*2));
	return cover(res,img,point(32-m.x,19-m.y));
}

double like(const IMG&img1,const IMG&img2)
{
	double cnt=0;
	for(int i=0;i<img1.n;i++)for(int j=0;j<img1.m;j++)cnt+=(img1.img[i][j]==img2.img[i][j]);
	return cnt/double(img1.n*img1.m);
}

char match(const IMG&img)
{
	double mx=0;
	int cur=-1;
	IMG tmp=balance(img);
	for(int i=0;i<lim;i++)for(int j=0;j<16;j++)
	{
		double deg=(like(tmp,randset[j][i]));
		if(deg>mx)mx=deg,cur=j;
	}
	return word[cur];
}

int calc(const string&s)
{
	vector<int>num;
	vector<char>op;
	bool dig=0;
	int tmp=0;
	
	auto cc=[&](char c)
	{
		int x,y;
		y=num.back(),num.pop_back();
		x=num.back(),num.pop_back();
		switch(c)
		{
			case'+':num.push_back(x+y);break;
			case'-':num.push_back(x-y);break;
			case'*':num.push_back(x*y);break;
			case'/':num.push_back(x/y);break;
		}
	};
	auto pri=[](char c)
	{
		switch(c)
		{
			case'(':return 0;break;
			case'+':case'-':return 1;break;
			case'*':case'/':return 2;break;
		}
		return-1;
	};
	
	for(const char c:s)
	{
		if(isdigit(c))tmp=tmp*10+c-48,dig=true;
		else
		{
			if(dig)num.push_back(tmp),tmp=0,dig=false;
			if(op.empty())op.push_back(c);
			else switch(c)
			{
				case'(':op.push_back(c);break;
				case')':
					while(op.back()!='(')cc(op.back()),op.pop_back();
					op.pop_back();
					break;
				default:
					while(!op.empty()&&pri(c)<=pri(op.back()))cc(op.back()),op.pop_back();
					op.push_back(c);
			}
		}
	}
	if(isdigit(s.back()))num.push_back(tmp);
	while(!op.empty())cc(op.back()),op.pop_back();
	return num.back();
	return 0;
}

void init(int T)
{
	for(int i=0;i<16;i++)wordset[i]=cover(IMG(65,38),wordset[i],wordpos[i]);
	
	double mnM=0.9,mxM=1,mnR,mxR,mnS,mxS;
	if(T<30)mnR=-2,mxR=2,mnS=mxS=0;
	else if(T<90)mnR=-10,mxR=10,mnS=-0.1,mxS=0.1;
	else mnR=-15,mxR=15,mnS=-0.1,mxS=0.1;
	
	uniform_real_distribution M(mnM,mxM),R(mnR,mxR),S(mnS,mxS);
	default_random_engine Gen(time(NULL));
	
	auto transform=[&](const IMG&img,double M,double Mx,double My,double R,double Sx,double Sy)
	{
		IMG ans(65,38);
		double radR=Rad(R);
		for(int i=0;i<65;i++)for(int j=0;j<38;j++)if(img.img[i][j])
		{
			dpoint p(j+0.5-19,i+0.5-32.5);
			p.x*=M*Mx,p.y*=M*My;
			dpoint tmp(p.x*cos(radR)-p.y*sin(radR),p.x*sin(radR)+p.y*cos(radR));
			p.x=tmp.x+Sy*tmp.y,p.y=tmp.y+Sx*tmp.x;
			point nxt(int(round(p.y+32.5-0.5)),int(round(p.x+19-0.5)));
			if(inIMG(nxt,point(65,38)))ans.img[nxt.x][nxt.y]=true;
		}
		return balance(denoise(ans));
	};
	
	for(int i=0;i<16;i++)for(int j=0;j<lim;j++)
	{
		double m=M(Gen),mx=M(Gen),my=M(Gen),r=R(Gen),sx=S(Gen),sy=S(Gen);
		randset[i][j]=transform(wordset[i],m,mx,my,r,sx,sy);
	}
}

IMG input;
vector<IMG>chars;

int main()
{
	ios::sync_with_stdio(false);
	int T,n,m;
	cin>>T>>n>>m;
	init(T);
	
	string s1,s2;
	while(cin>>s2)s1+=s2+' ';
	
	input=IMG(s1);
	input=denoise(input);
	
	chars=split(input);
	
	s2.clear();
	for(const IMG&i:chars)s2+=match(i);
	
	cout<<calc(s2);
}
2023/10/22 15:01
加载中...