图形周长(Image Perimeters)_搜索
本文最后更新于 1803 天前,其中的信息可能已经有所发展或是发生改变。
#include<iostream> 
#include<cstring>
using namespace std;
//char ch1[20][20][2];
char ch[20][20][2];
void make(int i,int j,int k){
	cout<<"执行make"<<endl;
	ch[i][j][1]=k+'0';
	cout<<"i "<<i<<" j "<<j<<" ch[i][j][0] "<<ch[i][j][0]<<" ch[i][j][1] "<<ch[i][j][1]<<endl;
	if(i-1>=0&&j-1>=0){
		if(ch[i-1][j-1][0]=='X'&&ch[i-1][j-1][1]=='0'){
			cout<<"执行make1"<<endl;
			make(i-1,j-1,k);
		}
	}
	if(i-1>=0){
		if(ch[i-1][j][0]=='X'&&ch[i-1][j][1]=='0'){
			cout<<"执行make2"<<endl;
			make(i-1,j,k);
		}
	}
	if(i-1>=0&&j+1<20){
		if(ch[i-1][j+1][0]=='X'&&ch[i-1][j+1][1]=='0'){
			cout<<"执行make3"<<endl;
			make(i-1,j+1,k);
		}
	}
	if(j-1>=0){
		if(ch[i][j-1][0]=='X'&&ch[i][j-1][1]=='0'){
			cout<<"执行make4"<<endl;
			make(i,j-1,k);
		}
	}
	if(j+1<20){
		if(ch[i][j+1][0]=='X'&&ch[i][j+1][1]=='0'){
			cout<<"执行make5"<<endl;
			make(i,j+1,k);
		}
	}
	if(i+1<20&&j-1>=0){
		if(ch[i+1][j-1][0]=='X'&&ch[i+1][j-1][1]=='0'){
			cout<<"执行make6"<<endl;
			make(i+1,j-1,k);
		}
	}
	if(i+1<20){
		if(ch[i+1][j][0]=='X'&&ch[i+1][j][1]=='0'){
			cout<<"执行make7"<<endl;
			make(i+1,j,k);
		}
	}
	if(i+1<20&&j+1<20){
		if(ch[i+1][j+1][0]=='X'&&ch[i+1][j+1][1]=='0'){
			cout<<"执行make8"<<endl;
			make(i+1,j+1,k);
		}
	}
	
	
}
void search(char ch[20][20][2],int m,int n){
	cout<<"执行search"<<endl;
	int k=1;
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			if(ch[i][j][0]=='X'&&ch[i][j][1]=='0'){
				cout<<"执行make准备"<<endl;
				make(i,j,k);
				k++;
				cout<<"执行make结束"<<endl;
			}				
		}
	}
}
void perimeters(int x,int y,int m,int n){
	char flag;
	int count=0;
	flag=ch[x-1][y-1][1];	
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			if(ch[i][j][1]==flag){
				int temp=count;
				if(i-1>=0){
					if(ch[i-1][j][0]=='.'){
						count++;
					}
				}
				else{
					count++;
				}
				if(i+1<m){
					if(ch[i+1][j][0]=='.'){
						count++;
					}
				}
				else{
					count++;
				}
				if(j-1>=0){
					if(ch[i][j-1][0]=='.'){
						count++;
					}
				}
				else{
					count++;
				}
				if(j+1<n){
					if(ch[i][j+1][0]=='.'){
						count++;
					}
				}
				else{
					count++;
				}
				cout<<"i="<<i<<" j="<<j<<" temp="<<(count-temp)<<endl;
			}
		}		
	}
	cout<<"perimeters="<<count<<endl;
}
int main(){
	int m,n,x,y;	
	cout<<"mnxy"<<endl;
	cin>>m>>n>>x>>y;
	while(m!=0&&n!=0&&x!=0&&y!=0){
		memset(ch,'0',sizeof(ch));
		for(int i=0;i<m;i++){
			char ch2[20];
			cin>>ch2;
			for(int j=0;j<n;j++){
				ch[i][j][0]=ch2[j];
			}
		}
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				cout<<ch[i][j][0];
			}
			cout<<endl;
		}
		cout<<endl;
		cout<<"执行search准备"<<endl;
		search(ch,m,n);
		cout<<"执行search结束"<<endl;
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				cout<<ch[i][j][0];
			}
			cout<<endl;
		}
		cout<<endl;
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				cout<<ch[i][j][1];
			}
			cout<<endl;
		}
		
		perimeters(x,y,m,n);
		
		cin>>m>>n>>x>>y;
	}
	return 0;
}
作者:Yuyy
博客:https://yuyy.info
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇