萌新写的代码,长但模块化
#include <stdio.h> #define ROW 100 #define COLUMN 100 int map[ROW][COLUMN]; /* 函数测试数据={1,1,1,0,0,1,1,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0};*/ int ArrayIndexInRange(int init,int incre,int max){ // 防止数组越界 int inRange=init+incre; if (inRange <0){ return 0; }else if (inRange>=max){ return max-1; }else {return inRange;}; }; enum Lights{Torch,Fluorite};//火把和萤石 //火把和萤石能照亮的范围(相对坐标) int TorchIllumination[13][2]={-2,0,-1,-1,-1,0,-1,1,0,-2,0,-1,0,0,0,1,0,2,1,-1,1,0,1,1,2,0}; int FluoriteIllumination[25][2]; int *pF=&FluoriteIllumination[0][0]; struct Light{ //光源结构体 int positionX,positionY; int type;//0为火把,1为萤石; }; int sum=0; void PlaceLight(struct Light t,int map[ROW][COLUMN],int row,int column){ //放置光源 printf("%d %d",t.positionX,t.positionY); int loopingTime =13;int *lightingRange=&TorchIllumination[0][0]; int x,y,offset[2]; if (t.type){loopingTime = 25;lightingRange = &FluoriteIllumination[0][0];} for (int lighting=0;lighting<loopingTime;lighting++){//printf("%d\n",map[t.positionX][t.positionY]);//for (int i =0;) offset[0]=lightingRange[2*lighting];offset[1]=lightingRange[2*lighting+1]; x=ArrayIndexInRange(t.positionX,offset[0],ROW); y=ArrayIndexInRange(t.positionY,offset[1],COLUMN); map[x][y]=1; }; }; void PrintfNumArray(int *p,int row,int column,int containerColumn){ // 筛选2维数组(地图)的有效部分并_输出 int item,line,wrap=column-1,spot; for (line=0;line<row;line++){ for (item=0;item<column;item++){ spot=p[line*containerColumn+item]; if (!spot){sum+=1;}; //printf("%2d",spot); //if (item%column==wrap) {printf("\n");} }; }; }; int main(){ for (int loop0=-2;loop0<3;loop0++){for (int loop1=-2;loop1<3;loop1++){*pF++=loop0;*pF++=loop1;};}; //PrintfNumArray(&map[0][0],3,4,5);//PrintfNumArray(&FluoriteIllumination[0][0],25,2,2);//printf("%d",TorchIllumination[2][1]); //struct Light t={3,4,0}; // *(&map[0][0]+34)=1; int n,m,k,positions[25][2]; int *_positions = &positions[0][0]; scanf("%d%d%d",&n,&m,&k); int lightsPlaced; for (int scaner=0;scaner<m+k;scaner++){ scanf("%d%d",&_positions[scaner*2],&_positions[scaner*2+1]); }; struct Light aTorch={0,0,0};struct Light aFluorite={0,0,1}; for (lightsPlaced=0;lightsPlaced<m;lightsPlaced++){ aTorch.positionX = _positions[lightsPlaced*2]; aTorch.positionY = _positions[lightsPlaced*2+1]; PlaceLight(aTorch,map,ROW,COLUMN); }; for (lightsPlaced=m;lightsPlaced<m+k;lightsPlaced++){ aFluorite.positionX = _positions[lightsPlaced*2]; aFluorite.positionY = _positions[lightsPlaced*2+1]; PlaceLight(aFluorite,map,ROW,COLUMN); }; //PlaceLight(t,map,ROW,COLUMN); PrintfNumArray(&map[0][0],n,n,COLUMN); printf("%d\n",sum); return 0; };
不过要注意点是,这是另写的代码。
确实有种MC的味道。
题目是以1,1为坐标原点的,所以应该把所有题目给的坐标的横纵坐标均减去一输入这个程序。并且题目只要输出有多少个点是黑暗的,并不要求显示地图的样子(所以那两行被注释掉了)。
标签:洛谷,Mc,int,火把,P1789,COLUMN,max,inRange,ROW From: https://www.cnblogs.com/embers-/p/16841134.html