This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<stdio.h> 
int H,W; 
int main(){ 
	scanf("%d%d",&H,&W); 
	int x,y,c; 
	printf("%d",2*H*W); 
	if(H%2==0){ 
		x=1; y=2; c=0; 
		printf("\n1 1 1\n1 1 0"); 
		for(;x!=1||y!=1;){ 
			printf("\n%d %d %d",x,y,c); 
			if(x==H){ 
				if(c==y%2)c=1-y%2; 
				else{ 
					if(y==1)x--,y=1,c=1; 
					else y--,c=y%2; 
				} 
			}else if(y==1){ 
				if(c)c=0; 
				else{ 
					x--; c=1; 
				} 
			}else if(x%2==1){ 
				if(!c)c=1; 
				else{ 
					if(y==W)x++,y=W,c=(W%2); 
					else y++,c=0; 
				} 
			}else if(x%2==0){ 
				if(c==y%2)c=1-y%2; 
				else{ 
					if(y==2)x++,y=2,c=0; 
					else y--,c=y%2; 
				} 
			} 
		} 
	}else if(W%2==0){ 
		x=1; y=2; c=0; 
		printf("\n1 1 1\n1 1 0"); 
		for(;x!=1||y!=1;){ 
			printf("\n%d %d %d",x,y,c); 
			if(x==1){ 
				if(!c)c=1; 
				else{ 
					if(y==W)x++,c=0; 
					else y++,c=0; 
				} 
			} else if(y==1){ 
				if(c)c=0; 
				else x--,c=1; 
			} else if(y%2==0){ 
				if(x==H){ 
					if(c)c=0; 
					else y--,c=1; 
				}else{ 
					if(c==x%2)c=1-c; 
					else c=(++x)%2; 
				} 
			} else if(y%2==1){ 
				if(x==2){ 
					if(c)c=0; 
					else y--,c=0; 
				}else{ 
					if(c)c=0; 
					else c=1,x--; 
				} 
			} 
		} 
	}else if(H==3){ 
		printf("\n3 1 1\n3 1 0\n2 1 1\n2 1 0\n1 1 1\n1 1 0\n1 2 0\n2 2 0\n1 2 1"); 
		x=1; y=3; c=0; 
		for(;x==1;){ 
			printf("\n%d %d %d",x,y,c); 
			if(!c)c=1; 
			else{ 
				if(y==W)x=2,y=W,c=1; 
				else y++,c=0; 
			} 
		} 
		for(int i=0;x!=3||y!=3||c!=1;i++){ 
			printf("\n%d %d %d",x,y,c); 
			switch((i/2)%4){ 
			case 0: 
				if(c)c=0; 
				else x++,c=0; 
				break; 
			case 1: 
				if(!c)c=1; 
				else y--,c=1; 
				break; 
			case 2: 
				if(c)c=0; 
				else x--,c=1; 
				break; 
			case 3: 
				if(c)c=0; 
				else y--,c=1; 
				break; 
			} 
		} 
		printf("\n2 3 1\n2 2 1\n2 3 0"); 
	}else{ 
		x=H; y=1; c=1; 
		for(;x!=1||y!=1||c!=0;){ 
			printf("\n%d %d %d",x,y,c); 
			if(c)c=0; 
			else x--,c=1; 
		} 
		for(;x!=1||y!=W-2||c!=0;){ 
			printf("\n%d %d %d",x,y,c); 
			if(x==H-2&&y==2&&!c)printf("\n%d 2 0",H-1); 
			if(y%2){ 
				if(c)c=0; 
				else{ 
					if(x==1)y++,c=0; 
					else x--,c=1; 
				} 
			}else{ 
				if(!c)c=1; 
				else{ 
					if(x==H-2)y++,c=1; 
					else x++,c=0; 
				}                    
			} 
		} 
		printf("\n%d %d %d",x,y,c); 
		y++; 
		for(;x<H-1;){ 
			printf("\n%d %d %d",x,y,c); 
			if(x%2){ 
				if(!c)c=1; 
				else{ 
					if(y==W)x++,c=y%2; 
					else y++,c=0; 
				} 
			}else{ 
				if(c==y%2)c=1-c; 
				else{ 
					if(y==W-1)x++,c=0; 
					else c=(--y)%2; 
				} 
			} 
		} 
		for(int i=0;y>2;i++){ 
			printf("\n%d %d %d",x,y,c); 
			switch((i/2)%4){ 
			case 0: 
				if(c)c=0; 
				else x++,c=0; 
				break; 
			case 1: 
				if(!c)c=1; 
				else y--,c=1; 
				break; 
			case 2: 
				if(c)c=0; 
				else x--,c=1; 
				break; 
			case 3: 
				if(c)c=0; 
				else y--,c=1; 
				break; 
			} 
		} 
		printf("\n%d 2 1\n%d 2 1\n%d 2 0",H,H-1,H); 
	} 
	return 0; 
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |