zoukankan      html  css  js  c++  java
  • 【题解】LuoguP1789 插火把

    一开始这个题目我用模拟做,这个样子:

    #include <iostream>
    using namespace std;
    bool light[101][101];
    int main(){
        ios::sync_with_stdio(false);
        int n,m,k,x,y,count=0;
        cin >> n >> m >> k;
        for(int i = 0;i < m;i ++){
            cin >> x >> y;
            light[x][y] = 1;
            if(x+1<=n&&y+1<=n)light[x+1][y+1] = 1;
            if(x-1>=1&&y-1>=1)light[x-1][y-1] = 1;
            if(x+1<=n&&y-1>=1)light[x+1][y-1] = 1;
            if(x-1>=1&&y+1<=n)light[x-1][y+1] = 1;
            if(x+1<=n)light[x+1][y] = 1;
            if(x+2<=n)light[x+2][y] = 1;
            if(x-1>=1)light[x-1][y] = 1;
            if(x-2>=1)light[x-2][y] = 1;
            if(y+2<=n)light[x][y+2] = 1;
            if(y+1<=n)light[x][y+1] = 1;
            if(y-1>=1)light[x][y-1] = 1;
            if(y-2>=1)light[x][y-2] = 1;
        }
        for(int i = 0;i < k;i ++){
            cin >> x >> y;
            for(int j = x;j <= x+5;j ++)for(int k1 = y;k1 <= 5+y;k ++)if(j<=n&&k1<=n)
    			light[j][k1]=1;
        }
        for(int j = 1;j <= n;j ++)for(int k1 = 1;k1 <= n;k1 ++){
            if(!light[j][k1]) count++;
        }
        cout << count; 
    }
    

    好极了,完美输出,结果......

    能不能快一点?我把&&换成了&,结果......

    没办法,那就来个记忆化:

    #include <iostream>
    using namespace std;
    bool light[101][101];
    int main(){
        int n,m,k,x,y,count=0;
        cin >> n >> m >> k;
        count = n*n ;
        for(int i = 0;i < m;i ++){
            cin >> x >> y;
            light[x][y] = 1;
            if(x+1<=n&&y+1<=n){
    			if(!light[x+1][y+1]){
    				count -- ;
    				light[x+1][y+1] = 1;
    			}
    		}
            if(x-1>=1&&y-1>=1){
            	if(!light[x-1][y-1]){
            		count --;
            		light[x-1][y-1] = 1;
    			}
    		}
            if(x+1<=n&&y-1>=1){
            	if(!light[x+1][y-1]){
    				count--;
    				light[x+1][y-1] = 1;
    			}
    		}
            if(x-1>=1&&y+1<=n){
            	if(!light[x-1][y+1]){
    				count--; 
            		light[x-1][y+1] = 1;
    			}
    		}
            if(x+1<=n){
            	if(!light[x+1][y]){
            		count--;	
    				light[x+1][y] = 1;
    			}  
    		}
            if(x+2<=n){
            	if(!light[x+2][y]){
            		count--;
    				light[x+2][y] = 1;
    			}
    		}
            if(x-1>=1){
            	if(!light[x-1][y]){
            		count --;
            		light[x-1][y] = 1;
    			}        	
    		}
            if(x-2>=1){
            	if(!light[x-2][y]){
    				count --;
    				light[x-1][y] = 1; 
    			} 
    		}
            if(y+2<=n){
            	if(!light[x][y+2]){
            		count --;
            		light[x][y+2] = 1;
    			}
    		}
            if(y+1<=n){
            	if(!light[x][y+1]){
            		count --;
            		light[x][y+1] = 1;
    			}
    		}
            if(y-1>=1){
            	if(!light[x][y-1]){
            		count --;
            		light[x][y-1] = 1;
    			}
    		}
            if(y-2>=1){
            	if(!light[x][y-2]){
            		count --;
            		light[x][y-1] = 1;
    			}
    		}
        }
        for(int i = 0;i < k;i ++){
            cin >> x >> y;
            for(int j = x;j <= x+5;j ++)for(int k1 = y;k1 <= 5+y;k ++)if(j<=n&&k1<=n)
            	if(!light[j][k1]){
            		count --;
            		light[j][k1] = 1;
    			}
        }
        cout << count-1; 
    }
    
  • 相关阅读:
    我就是想找个人聊聊天,说说我这近四年来的经历-02
    我就是想找个人聊聊天,说说我这近四年来的经历
    Padas交叉表新增二级分类小计
    superset开启本地缓存filesystem
    Superset连接Impala数据源
    Python实现网站注册验证码生成类
    Python爬虫原理
    Superset导出pivot_table到excel
    Superset导出CSV文件中文或日文乱码
    Linux下如何高效删除一个几十G的文本文件的最后一行或几行
  • 原文地址:https://www.cnblogs.com/sdltf/p/12456858.html
Copyright © 2011-2022 走看看