算法8

描述

螺旋数组

例子
Input:
	7 9
Output:
	1	2	3	4	5	6	7	8	9	
	28	29	30	31	32	33	34	35	10	
	27	48	49	50	51	52	53	36	11	
	26	47	60	61	62	63	54	37	12	
	25	46	59	58	57	56	55	38	13	
	24	45	44	43	42	41	40	39	14	
	23	22	21	20	19	18	17	16	15
思路

先将数组赋值为-1,使用一个变量记录当前方向(上下左右),当某个方向走不动时,换下一个方向。

代码
public class Question008 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();

        int [][] array = new int [m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                array[i][j] = -1;
            }
        }
        int x = 0, y = 0;
        int count = 1;
        // 右1 下2 左3 上4
        int last_t = 1;
        while (count <= m*n) {
            array[x][y] = count++;
            if(last_t == 1) {
                if (y+1<n && array[x][y+1] < 0){
                    y++;
                } else {
                    last_t = 2;
                    if (x+1 < m && array[x+1][y] < 0) {
                        x++;
                    }else {
                        break;
                    }
                }
                //下
            } else if (last_t == 2) {
                if(x+1 < m && array[x+1][y] < 0) {
                    x++;
                } else {
                    last_t = 3;
                    if (y-1 >= 0 && array[x][y-1] < 0) {
                        y--;
                    } else {
                        break;
                    }
                }
                //左
            } else if (last_t == 3) {
                if(y-1 >= 0 && array[x][y-1] < 0) {
                    y--;
                } else {
                    last_t = 4;
                    if (x-1 >= 0 && array[x-1][y] < 0) {
                        x--;
                    } else {
                        break;
                    }
                }
            }
            //上
            else {
                if(x-1 >= 0  && array[x-1][y] < 0) {
                    x--;
                } else {
                    last_t = 1;
                    if (y+1 < n  && array[x][y+1] < 0) {
                        y++;
                    } else {
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(array[i][j]+"\t");
            }
            System.out.println();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
include using namespace std; struct node{ int nodesun[4][4]; int pre; //上一步在队列的位置 int flag ; //步数标识,表示当前的步数为有效的 int value; //与目标的差距 int x,y; //空格坐标 }queue[1000]; //移动方向数组 int zx[4]={-1,0,1,0}; int zy[4]={0,-1,0,1}; //当前步数 int top; int desti[4][4];//目标状态 int detect(struct node *p)//检查是否找到 {int i,j; for(i=1;i<4;i++) for(j=1;jnodesun[i][j]!=desti[i][j]) return 0; return 1; } //打印 void printlj() {int tempt; int i,j; tempt=top; while(tempt!=0) { for(i=1;i<4;i++) for(j=1;j<4;j++) {cout<<queue[tempt].nodesun[i][j]; if(j==3) cout<<" "<<endl; } tempt=queue[tempt].pre; } } //现在状态与目标状态有多少个不同位置 int VALUE(struct node *p) {int count=0; int i,j; for(i=1;i<4;i++) for(j=1;jnodesun[i][j]!=desti[i][j]) count++; return count; } void main() { //初始化 int i,j,m,n,f; int min=10; int temp,find=0,minnumber; top=1; for(i=1;i<4;i++) for(j=1;j<4;j++) {cout<<"请输入第"<<i<<"行"<<"第"<<j<<"列的值"<>temp; queue[1].nodesun[i][j]=temp; } cout<<"请输入初始状态的空格的位置(行)"<>temp; queue[1].x=temp; cout<<"请输入初始状态的空格的位置(列)"<>temp; queue[1].y=temp; queue[1].value=VALUE(&queue[1]); queue[1].pre=0; //上一步在队列的位置 queue[1].flag=0; //目标状态 for(i=1;i<4;i++) for(j=1;j<4;j++) {cout<<"请输入目标状态第"<<i<<"行"<<"第"<<j<<"列的值"<>temp; desti[i][j]=temp; } //根据估价函数 while(!find&&top>0) { for(i=1;i<=top;i++) //////////////////////////////////////////// //min为上一图与目标图有多少个元素不相同,queue[i]为当前图与目标图有多少个元素不相同通过这两个数的比较,就可以得出当前图较之上一图向目标图接近同时把当前的i记录下来进行下一步比较 {if(queue[i].value<min&&queue[i].flag==0) {minnumber=i;// min=queue[i].value; //还有多少不同的位数 } } queue[minnumber].flag=1; //表示此位有效 ////////////////////////////////////// // for(f=0;f=1&&i=1&&j<=3) {top++; ///////////////////////////////////////////// //位置交换 queue[top]=queue[minnumber]; queue[top].nodesun[m][n]=queue[minnumber].nodesun[i][j]; queue[top].nodesun[i][j]=0; /////////////////////////////////////// //空格移动方向 queue[top].x=i; queue[top].y=j; /////////////////////////////////////// queue[top].pre=minnumber; //上一步在队列的位置 queue[top].value=VALUE(&queue[top]); //有多少位与目标不同 queue[top].flag=0; //标识位初始化 if(detect(&queue[top])) //检查是否为目标 {printlj(); //打印 find=1; //设找到标识位 break; } } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值