其他分享
首页 > 其他分享> > Atcoder Beginner Contest 213题解

Atcoder Beginner Contest 213题解

作者:互联网

Atcoder Beginner Contest 213题解

A - Bitwise Exclusive Or

题意:给定两个数\(A,B\),求\(C\)使\(A\mbox{ xor }C=B\)。
思路:\(A\mbox{ xor }C=B\Leftrightarrow A\mbox{ xor }B=C\)。
代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    cout<<(a^b)<<endl;
    return 0;
}

B - Booby Prize

E - Stronger Takahashi

思路:分类dijkstra。
代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=550;
const int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
const int ddx[4][6]={-2,-2,-2,-1,-1,-1,2,2,2,1,1,1,-1,0,1,-1,0,1,-1,0,1,-1,0,1};
const int ddy[4][6]={-1,0,1,-1,0,1,-1,0,1,-1,0,1,-2,-2,-2,-1,-1,-1,2,2,2,1,1,1};
int h,w,dist[maxn][maxn];
char a[maxn][maxn];
priority_queue<pair<int,pair<int,int> > > pq;
bool isvalid(int x,int y)
{
	return x>=1&&x<=h&&y>=1&&y<=w;
}
int main()
{
	cin>>h>>w;
	for(int i=1;i<=h;i++) for(int j=1;j<=w;j++) cin>>a[i][j];
	memset(dist,0x3f,sizeof(dist));
	dist[1][1]=0;
	pq.push(make_pair(0,make_pair(1,1)));
	while(!pq.empty())
	{
		int d=-pq.top().first,x=pq.top().second.first,y=pq.top().second.second;
		pq.pop();
		if(d!=dist[x][y]) continue;
		for(int i=0;i<4;i++)
		{
			int tx=x+dx[i],ty=y+dy[i];
			if(!isvalid(tx,ty)) continue;
			int v=(a[tx][ty]=='#');
			if(dist[tx][ty]>d+v)
			{
				dist[tx][ty]=d+v;
				pq.push(make_pair(-dist[tx][ty],make_pair(tx,ty)));
			}
			for(int j=0;j<6;j++)
			{
				int ttx=x+ddx[i][j],tty=y+ddy[i][j];
				if(!isvalid(ttx,tty)) continue;
				if(dist[ttx][tty]>d+1)
				{
					dist[ttx][tty]=d+1;
					pq.push(make_pair(-dist[ttx][tty],make_pair(ttx,tty)));
				}
			}
		}
	}
	cout<<dist[h][w]<<endl;
	return 0;
}

标签:Atcoder,pq,dist,213,int,题解,make,maxn,pair
来源: https://www.cnblogs.com/jpy-cpp/p/15116287.html