CCFCSP201512-3 画图

该代码实现了一个连通图的递归判断和线条绘制功能。通过DFS遍历图形,遇到符合条件的空位则标记并继续搜索。同时,根据输入坐标绘制水平或垂直线条。程序读取网格大小、线条数量及操作,进行相应操作并输出最终网格状态。
摘要由CSDN通过智能技术生成

PS:本题注意横纵坐标,[1,3]是哪个点?
连通图的递归判断中 :1. void函数,这里向4个方向递归可以4个if,也可以for,建议for
2.不符合条件不需要跳出。即不需要下面代码

if(!func(a,b,c))
	return;
	

ac代码:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int m,n,q;
vector<string> v[105];
int arr[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int func(int x,int y,string c){
	if(x>=0 && x<m && y>=0 && y<n &&v[y][x]!="|" && v[y][x]!="-" && v[y][x]!="+" && v[y][x]!=c)
	return 1;
	return 0;
}
void dfs(int x,int y,string c){	

		v[y][x]=c;
//		cout<<x<< " "<<y<<" "<<v[x][y]<<endl;
		for(int i=0;i<4;i++){
			int nx=x+arr[i][0];
			int ny=y+arr[i][1];
			if(func(nx,ny,c))
				dfs(nx,ny,c);
		}
		}



void line(int x1,int y1,int x2 ,int y2){
//	cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<endl;
	int op=0;
	if(x1==x2)
	op=1;
	if(y1==y2)
	op=2;
//	cout<<"op"<<op<<endl;
	if(op==1){
		int res=y1;
		y1=min(res,y2);
		y2=max(res,y2);
//		cout<<"y1 y2 "<<y1<<y2<<endl;
		for(int j=y1;j<=y2;j++){
			if(v[j][x1]=="-" || v[j][x1]=="+")
				v[j][x1]="+";
			else
				v[j][x1]="|"; 
	}
}
	else if(op==2){
		int res=x1;
		x1=min(res,x2);
		x2=max(res,x2);
//		cout<<"x1 x2 "<<x1<<x2<<endl;
		for(int i=x1;i<=x2;i++){
			if(v[y1][i]=="|"  || v[y1][i]=="+")
				v[y1][i]="+";
			else
				v[y1][i]="-"; 
	}
}
}
int main(){
	cin>>m>>n>>q;	
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			v[i].push_back(".");
		}
	}
	for(int i=0;i<q;i++){
		int opr;
		cin>>opr;
		if(opr==1){
			int x,y;
			string c;
			cin>>x>>y>>c;
			dfs(x,y,c);
		}
		else {
//			cout<<"xian"<<endl;
			int x1,y1,x2,y2;
			cin>>x1>>y1>>x2>>y2;
			line(x1,y1,x2,y2);
		}
	
	}
for(int i=n-1;i>=0;i--){
		for(int j=0;j<m;j++){
			cout<<v[i][j];
		}
		printf("\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值