AcWing春季每日一题 Week3

文章目录

  • 1.AcWing 1683. 困牛放牧
  • 2.AcWing 1470. 水桶传递队列
  • 3.AcWing 1761. 阻挡广告牌
  • 4.AcWing 1749. 阻挡广告牌 II
  • 5.AcWing 1737. 传送
  • 6.AcWing 1725. 组队井字游戏

1.AcWing 1683. 困牛放牧

题目

AC代码:

#include 
#include 
#include 
using namespace std;
int main(){
	int a,b,c,d1,d2;
	scanf("%d %d %d",&a,&b,&c);
	d1=b-a-1;
	d2=c-b-1;
	if(d1 == 0 && d2 == 0)
		printf("0\n0\n");
	else if(d1 == 0 || d2 == 0){
		int t = max(d1,d2);
		if(t == 1)
			printf("1\n1\n");
		else{
			printf("2\n%d\n",t);
		}
	}
	else{
	    if(d1 > d2)
	    	swap(d1,d2);
	    if(d1 == 1)
	    printf("1\n%d\n",d2);
	    else
	    printf("2\n%d\n",d2);
	}
	return 0;
} 

2.AcWing 1470. 水桶传递队列

题目

AC代码:

//解法一:广搜版
#include 
#include 
#include 
#include 
using namespace std;
struct node{
	int x;
	int y;
	int f;
}st,ed;
char mapp[11][11];
int to[4][2]={0,1,0,-1,1,0,-1,0};
int book[11][11];
int bfs(){
	memset(book,0,sizeof(book));
	node now,nex;
	queue<node> q;
	ed.f = 0;
	book[ed.x][ed.y]=1;
	q.push(ed);
	while(!q.empty()){
		now = q.front();
		q.pop();
		if(now.x == st.x && now.y == st.y)
			return now.f-1;
		for(int i=0;i<4;i++){
			nex.x = now.x + to[i][0];
			nex.y = now.y + to[i][1];
			nex.f = now.f + 1;
			if(mapp[nex.x][nex.y] == 'R' || nex.x<0||nex.x>=10||nex.y<0||nex.y>=10||book[nex.x][nex.y]==1)
				continue;
			book[nex.x][nex.y]=1;
			q.push(nex);				
		}	
	}
	return 0;
}
int main(){
	for(int i=0;i<10;i++)
		scanf("%s",mapp[i]);
	for(int i=0;i<10;i++){
		for(int j=0;j<10;j++){
			if(mapp[i][j]=='B'){
				st.x = i;
				st.y = j;
			}
			if(mapp[i][j]=='L'){
				ed.x = i;
				ed.y = j;
			}
		}
	}
	printf("%d\n",bfs());
	
	return 0;
}
//解法二:易懂版
#include 
using namespace std;

string g[10];
int bx, by, lx, ly, rx, ry;

int main()
{
    for (int i = 0; i < 10; i++)    cin >> g[i];

    for (int i = 0; i < 10; i++)
        for (int j = 0; j < 10; j++)
        {
            if (g[i][j] == 'B') bx = i, by = j;  // 记录牛棚的坐标
            if (g[i][j] == 'L') lx = i, ly = j;  // 记录湖的坐标
            if (g[i][j] == 'R') rx = i, ry = j;  // 记录岩石的坐标
        }

    int b2l = abs(bx - lx) + abs(by - ly);  // 牛棚到湖的曼哈顿距离
    int b2r = abs(bx - rx) + abs(by - ry);  // 牛棚到岩石的曼哈顿距离
    int r2l = abs(rx - lx) + abs(ry - ly);  // 岩石到湖的曼哈顿距离

    if ((bx == lx || by == ly) && b2l == b2r + r2l) 
        cout << b2l + 1 << endl;
    else 
        cout << b2l - 1 << endl;

    return 0;
}
//解法三:~~~
#include
using namespace std;
char s[11];
int sx,sy,ex,ey,rx,ry;
int main()
{
    for(int i=1;i<=10;i++){
        cin>>s+1;
        for(int j=1;j<=10;j++){
            if(s[j]=='R')
            	rx=i,ry=j;//记录石头的x,y坐标
            else if(s[j]=='B')
            	ex=i,ey=j;//记录牛棚的x,y坐标
            else if(s[j]=='L')
            	sx=i,sy=j;//记录湖的x,y坐标
        }
    }
    int ans=abs(sx-ex)+abs(sy-ey)-1;
    //判断石头是否在两者中间
    //(syey 
    //						  ②sy>ry && ry
    
    if((rx==sx&&sx==ex&&!(sy<ry^ry<ey))||(ry==sy&&sy==ey&&!(sx<rx^rx<ex)))
    	ans+=2//理解一下还是能懂的
    cout<<ans;
}

3.AcWing 1761. 阻挡广告牌

题目

比较好理解的版本,染色法,注意左右只能去一个,上下也是

AC代码:

#include 
#include 
#include 
using namespace std;
int x1,y1,x2,y2;
int mapp[2100][2100];
int main(){
	memset(mapp,0,sizeof(mapp));
	for(int i=1;i<=3;i++){
		scanf("%d %d %d %d",&x1,&y1,&x2,&y2);			
		x1 += 1000;
		x2 += 1000;
		y1 += 1000;
		y2 += 1000;
		for(int j=x1;j<x2;j++)
			for(int k=y1;k<y2;k++){
				if(i==3)
					mapp[j][k] = 0;
				else
					mapp[j][k] = 1;
			}
	}
	int sum=0;
	for(int i=0;i<=2000;i++){
		for(int j=0;j<=2000;j++){
			if(mapp[i][j] == 1)
				sum++;
		}
	}
	printf("%d\n",sum);
	return 0;
} 

4.AcWing 1749. 阻挡广告牌 II

题目

AC代码:

#include 
int xx1,yy1,xx2,yy2;
int xx3,yy3,xx4,yy4;
int mapp[2100][2100];
bool check(int x,int y){
	if(x > xx1 && x < xx2 && y > yy1 && y < yy2)
		return true;
	else
		return false;
}
int main(){
	int sum = 0;
	scanf("%d %d %d %d",&xx1,&yy1,&xx2,&yy2);	
	scanf("%d %d %d %d",&xx3,&yy3,&xx4,&yy4);		
	xx1 += 1000; xx2 += 1000;
	yy1 += 1000; yy2 += 1000;
	xx3 += 1000; xx4 += 1000;
	yy3 += 1000; yy4 += 1000;
	for(int j=xx1;j<xx2;j++)
	for(int k=yy1;k<yy2;k++)
		mapp[j][k] = 1,sum++;
	
	for(int j=xx3;j<xx4;j++)
	for(int k=yy3;k<yy4;k++)
		mapp[j][k] = 0;
	
	
	
	if(check(xx3,yy3) || check(xx3,yy4) || check(xx4,yy3) || check(xx4,yy4) || (xx3 < xx2 && xx3 > xx1 && xx4 < xx2 && xx4 > xx1 ) || (yy3 < yy2 && yy3 > yy1 && yy4 < yy2 && yy4 > yy1)){
		printf("%d\n",sum);
			return 0;
			
	}
	sum = 0 ;
	for(int i=0;i<=2000;i++)
		for(int j=0;j<=2000;j++)
			if(mapp[i][j] == 1)
				sum++;
	
	printf("%d\n",sum);
	return 0;
} 

5.AcWing 1737. 传送

题目

AC代码:

#include 
using namespace std;
int main(){
	int a,b,x,y;
	scanf("%d %d %d %d",&a,&b,&x,&y);
	if(a > b)
		swap(a,b);
	if(x > y)
		swap(x,y);
	printf("%d\n",min((b-a),abs(x-a)+abs(y-b))); 
	return 0;
}

6.AcWing 1725. 组队井字游戏

题目

AC代码:

#include 
using namespace std;

set<set<char>> ans[2];
string g[3];

void insert(vector<vector<int>> ps){
	set<char> S;
	for(auto& p : ps){
		int x = p[0],y= p[1];
		S.insert(g[x][y]);
	}
	if(S.size() == 1)
		ans[0].insert(S);
	else if(S.size() == 2)
		ans[1].insert(S);
}
int main()
{
	for(int i=0; i<3; i++) cin>>g[i];
	//行
	for(int i=0; i<3; i++)
	    insert({{i, 0}, {i, 1}, {i, 2}});
	//列
	for(int i=0; i<3; i++)
	    insert({{0, i}, {1, i}, {2, i}});
	//两条对角线
	insert({{0, 0}, {1, 1}, {2, 2}});
	insert({{2, 0}, {1, 1}, {0, 2}});
	
	cout<<ans[0].size()<<endl<<ans[1].size();
	
    return 0;
}


你可能感兴趣的:(AcWing,c++,算法)