编程语言
首页 > 编程语言> > 机考0分算法

机考0分算法

作者:互联网

image
睿智题一个,考试时分24种类,分了1小时0分

#include <stdio.h>
#include <math.h>
int main()
{
	int m,n,t;
	scanf("%d %d %d",&m,&n,&t);
	int c=0;
	int k=(m+1)*(n+1);
	int ch[k];
	for(int i=0;i<=n;i++){
		ch[i]=1;
	}
//	int f=0;
//	for(int i=0;i<=m*n;i+=n){
//		ch[f*(n+1)]=1;
//		f++;
//	}
	for(int i=0;i<=m;i++){
		ch[i*(n+1)]=1;
	}
	for(int i=0;i<t;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		if(x==1&&y==1){
			c++;
			if(ch[2]==0){
				c++;
				ch[2]=1;
			}
			if(ch[1+n]==0){
				c++;
				ch[1+n]=1;
			}
			if(ch[2+n]==0){
				c++;
				ch[2+n]=1;
			}
		}
		else if(x==1&&y==m){
			c++;
			if(ch[n*(m-1)+1]==0){
				ch[n*(m-1)+1]=1;
				c++;
			}
			if(ch[n*(m-1)+1-m]==0){
				ch[n*(m-1)+1-m]=1;
				c++;
			}
			if(ch[n*(m-1)-m]==0){
				ch[n*(m-1)-m]=1;
				c++;
			}
		}
		else if(x==n&&y==1){
			c++;
			if(ch[m*(n-1)+1]==0){
				ch[m*(n-1)+1]=1;
				c++;
			}
			if(ch[m*(n-1)+1-n]==0){
				ch[m*(n-1)+1-n]=1;
				c++;
			}
			if(ch[m*(n-1)-n]==0){
				ch[m*(n-1)-n]=1;
				c++;
			}
		}
		else if(x==n&&y==m){
			c++;
			if(ch[m*n-1]==0){
				ch[m*n-1]=1;
				c++;
			}
			if(ch[m*n-1-m]==0){
				ch[m*n-1-m]=1;
				c++;
			}
			if(ch[m*n-m]==0){
				ch[m*n-m]=1;
				c++;
			}
		}
		else if(x==1&&y>=2&&y<=m-1){
			c++;
			if(ch[m*y+2]==0){
				ch[m*y+2]=1;
				c++;
			}
			if(ch[m*y+1+m]==0){
				ch[m*y+1+m]=1;
				c++;
			}
			if(ch[m*y+1-m]==0){
				ch[m*y+1-m]=1;
				c++;
			}
		}
		else if(y==1&&x>=2&&x<=n-1){
			c++;
			if(ch[x+1]==0){
				ch[x+1]=1;
				c++;
			}
			if(ch[x-1]==0){
				ch[x-1]=1;
				c++;
			}
			if(ch[x+m]==0){
				ch[x+m]=1;
				c++;
			}
		}
		else if(x==n&&y>=2&&y<=m-1){
			c++;
			if(ch[m*y+m-1]==0){
				ch[m*y+m-1]=1;
				c++;
			}
			if(ch[m*y+2*m]==0){
				ch[m*y+2*m]=1;
				c++;
			}
			if(ch[m*y]==0){
				ch[m*y]=1;
				c++;
			}
		}
		else if(y==m&&2<=x&&x<=n-1){
			c++;
			if(ch[y*(m-1)+x+1]==0){
				ch[y*(m-1)+x+1]=1;
				c++;
			}
			if(ch[y*(m-1)+x-1]==0){
				ch[y*(m-1)+x-1]=1;
				c++;
			}
			if(ch[y*(m-1)+x-m]==0){
				ch[y*(m-1)+x-m]=1;
				c++;
			}
		}
		else{
			c+=1;
			if(ch[m+m*y+1]==0){
				ch[m+m*y+1]=1;
				c++;
			}
			if(ch[m+m*y-1]==0){
				ch[m+m*y-1]=1;
				c++;
			}
			if(ch[m+m*y+m]==0){
				ch[m+m*y+m]=1;
				c++;
			}
			if(ch[m+m*y-m]==0){
				ch[m+m*y-m]=1;
				c++;
			}
			if(ch[m+m*y+1+m]==0){
				ch[m+m*y+1+m]=1;
				c++;
			}
			if(ch[m+m*y+1-m]==0){
				ch[m+m*y-1]=1;
				c++;
			}
			if(ch[m+m*y-1+m]==0){
				ch[m+m*y-1+m]=1;
				c++;
			}
			if(ch[m+m*y-1-m]==0){
				ch[m+m*y-1-m]=1;
				c++;
			}
		}
	}
	printf("%d",c);
}

然而只要在判断灯亮不亮的时候判断数组越不越界就行了。100分代码

#include <stdio.h>
#include <math.h>
int main()
{
	int m,n,t;
	scanf("%d %d %d",&m,&n,&t);
	int a[m][n];
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			a[i][j]=0;
		}
	}
	int cnt=0;
	for(int i=0;i<t;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		int xx=x-1;
		int yy=y-1;
		if(a[xx][yy]==0){
			a[xx][yy]=1;
			cnt++;
		}
		if(a[xx+1][yy]==0&&0<=xx+1&&xx+1<=m-1&&0<=yy&&yy<=n-1){
			cnt++;
			a[xx+1][yy]=1;
		}
		if(a[xx-1][yy]==0&&0<=xx-1&&xx-1<=m-1&&0<=yy&&yy<=n-1){
			cnt++;
			a[xx-1][yy]=1;
		}
		if(a[xx][yy+1]==0&&0<=xx&&xx<=m-1&&0<=yy+1&&yy+1<=n-1){
			cnt++;
			a[xx][yy+1]=1;
		}
		if(a[xx][yy-1]==0&&0<=xx&&xx<=m-1&&0<=yy-1&&yy-1<=n-1){
			cnt++;
			a[xx][yy-1]=1;
		}
		if(a[xx+1][yy+1]==0&&0<=xx+1&&xx+1<=m-1&&0<=yy+1&&yy+1<=n-1){
			cnt++;
			a[xx+1][yy+1]=1;
		}
		if(a[xx+1][yy-1]==0&&0<=xx+1&&xx+1<=m-1&&0<=yy-1&&yy-1<=n-1){
			cnt++;
			a[xx+1][yy-1]=1;
		}
		if(a[xx-1][yy+1]==0&&0<=xx-1&&xx-1<=m-1&&0<=yy+1&&yy+1<=n-1){
			cnt++;
			a[xx-1][yy+1]=1;
		}
		if(a[xx-1][yy-1]==0&&0<=xx-1&&xx-1<=m-1&&0<=yy-1&&yy-1<=n-1){
			cnt++;
			a[xx-1][yy-1]=1;
		}
	}
	printf("%d",cnt);
}

呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜

标签:main,int,呜呜,机考,算法,&&,include,scanf
来源: https://www.cnblogs.com/kingbridge/p/15732646.html