201412-2 Z字形扫描

Z字形扫描

201412-2

语言:C++
分数:100

代码


#include <iostream>

using namespace std;

int a[500][500];

int main()
{
	int N;
	cin>>N;
	int i,j;
	
	for(i=0;i<N;i++)
	    for(j=0;j<N;j++)
	        cin>>a[i][j];

	int dir=-1;//dir means direaction -1:down |1:up|0:striagt
	i=0;
	j=0;
	int N_flag=N%2;
	        
	while(j!=N-1||i!=N-1)
	{
		cout<<a[i][j]<<" ";
		
		if((i==0&&j%2==0)||(i==N-1&&j%2==N_flag))
		{
            if(j==N-1&&dir==1)
            {
                i++;
                dir=-1;
				continue;	
			}
		    j++;
		    if(i==0)
		        dir=-1;
		    else
		        dir=1;
		    continue;
		}
		    
		else if((j==N-1&&(i+1)%2==N_flag)||(j==0&&i%2==1))
		{
			if(i==N-1&&dir==-1)
			{
				j++;
				dir=1;
				continue;
			}  
			i++;
			if(j==0)
			    dir=1;
			else
			    dir=-1;
			continue;
		}
		
		else
		{
			if(dir==-1)
			{
				i++;
				j--;
			}
			else
			{
				i--;
				j++;
			}
		}	
			
	}
	cout<<a[i][j]<<endl;
	
	return 0;
}
上一篇:201412-2 z字形扫描


下一篇:201412-2 Z字形扫描