返回题解分享
讨论 / 题解分享/ 帖子详情

蛇形填数(结果填空) - 题解

模拟蛇形前进

#include <cstdio>
#include <cmath>
#include<iostream>
using namespace std;
int a[200][200];
int main(){
	int x=1,y=1;
	int count=0;
	for(int i=1;i<200;i++){  //上边界和左边界为0,其他均为1 
		for(int j=1;j<200;j++){
			a[i][j]=1;
		}
	}
	int pre1=1,pre2=-1;
	int n=1;
	while(1){	  //x,y是坐标,蛇形前进 
		if(x==20&&y==20){
			cout<<n;
			break;
		}
		
		if(a[x][y-1]==0){  //碰到 上边界
			y++;
			pre1=-pre1;
			pre2=-pre2;
			n+=2;
		}
		else if (a[x-1][y]==0){  //碰到左边界
			x++;
			pre1=-pre1;
			pre2=-pre2;
			n+=2;
		}
		else{
			x+=pre1;y+=pre2;
			n++;
		}			
		
	}
	
}
0 回复 0 转发 1 喜欢 2 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!