codeparrot/codecomplex huggingface dataset:
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


public class FireAgain {

	Scanner in;
	PrintWriter out;
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		new FireAgain().run();

	}
	
	void run() throws IOException {
		in = new Scanner(new FileReader("input.txt"));
        out = new PrintWriter(new FileWriter("output.txt"));

		solve();

		in.close();
		out.close();
	}

	private void solve() {
		// TODO Auto-generated method stub
		int N = in.nextInt();
		int M = in.nextInt();
		int[][] burn = new int[N + 1][M + 1];
		int K = in.nextInt();
		
		int[] qx = new int[N * M];
		int[] qy = new int[N * M];
		
		int first = 0;
		int last = 0;
		for (int i = 0; i < K; i ++){
			qx[last] = in.nextInt();
			qy[last] = in.nextInt();
			burn[qx[last]][qy[last]] = 1;
			last ++;
		}
		
		while (first < last){
			int x = qx[first];
			int y = qy[first];
			if (x - 1 > 0 && burn[x - 1][y] == 0){
				burn[x - 1][y] = 1;
				qx[last] = x - 1;
				qy[last] = y;
				last ++;
			}
			if (y - 1 > 0 && burn[x][y - 1] == 0){
				burn[x][y - 1] = 1;
				qx[last] = x;
				qy[last] = y - 1;
				last ++;
			}
			if (x + 1 <= N && burn[x + 1][y] == 0){
				burn[x + 1][y] = 1;
				qx[last] = x + 1;
				qy[last] = y;
				last ++;
			}
			if (y + 1 <= M && burn[x][y + 1] == 0){
				burn[x][y + 1] = 1;
				qx[last] = x;
				qy[last] = y + 1;
				last ++;
			}
			first ++;
		}
		
		out.println(qx[last - 1] + " " + qy[last - 1]);
	}
}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class FireAgain {
	static int n;
	static int m;

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader r = new BufferedReader(new FileReader("input.txt"));
		String s = r.readLine();
		String[] sp = s.split(" ");
		n = new Integer(sp[0]);
		m = new Integer(sp[1]);
		boolean[][] v = new boolean[n][m];
		r.readLine();
		s = r.readLine();
		sp = s.split(" ");
		Queue<Integer> q = new LinkedList<Integer>();
		for (int i = 0; i < sp.length; i += 2) {
			v[new Integer(sp[i]) - 1][new Integer(sp[i + 1]) - 1] = true;
			q.add(new Integer(sp[i]) - 1);
			q.add(new Integer(sp[i + 1]) - 1);
		}

		int[] dx = { 1, -1, 0, 0 };
		int[] dy = { 0, 0, 1, -1 };
		int lx = -1;
		int ly = -1;
		while (!q.isEmpty()) {
			int x = q.remove();
			int y = q.remove();
			lx = x;
			ly = y;
			for (int i = 0; i < dy.length; i++) {
				int nx = x + dx[i];
				int ny = y + dy[i];
				if (valid(nx, ny) && !v[nx][ny]) {
					v[nx][ny] = true;
					q.add(nx);
					q.add(ny);
				}
			}
		}
		lx++;
		ly++;
		BufferedWriter wr=new BufferedWriter(new FileWriter("output.txt"));
		wr.write(""+lx + " " + ly);
		wr.newLine();
		wr.close();
		
	}

	private static boolean valid(int nx, int ny) {
		return nx >= 0 && nx < n && ny >= 0 && ny < m;
	}
}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import static java.lang.Math.*;
import java.io.*;
import java.math.*;
import java.util.*;

public class Solution implements Runnable {

	public static void main(String... strings) throws InterruptedException {
		new Thread(new Solution()).start();
	}

	BufferedReader in;
	PrintWriter out;
	StringTokenizer st;

	String next() throws Exception {
		if (st == null || !st.hasMoreElements())
			st = new StringTokenizer(in.readLine());
		return st.nextToken();
	}

	int nextInt() throws Exception {
		return Integer.parseInt(next());
	}

	double nextDouble() throws Exception {
		return Double.parseDouble(next());
	}

	long nextLong() throws Exception {
		return Long.parseLong(next());
	}

	@Override
	public void run() {
		try {
			in = new BufferedReader(new FileReader("input.txt"));
			out = new PrintWriter(new FileWriter("output.txt"));
			solve();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			out.close();
		}
	}
	int n, m, k, xor = 0;
	boolean[][] used;
	HashSet<Long> [] set;
	void solve() throws Exception {
		n = nextInt();
		m = nextInt();
		k = nextInt();
		used = new boolean[n][m];
		set = new HashSet[2];
		for(int i = 0; i < 2; set[i++] = new HashSet<Long>());
		
		for(int i = 0; i < k; i++){
			int x = nextInt()-1, y = nextInt()-1;
			used[x][y] = true;
			set[0].add(10000L*x + y);
		}
		for (;;xor ^= 1){
			set[xor^1].clear();
			int ansx = -1, ansy = -1;
			for (long i : set[xor]){
				int x = (int)(i/10000), y = (int)(i%10000);
				if (ansx < 0){
					ansx = x+1;
					ansy = y+1;
				}
				add(x+1, y);
				add(x-1, y);
				add(x, y+1);
				add(x, y-1);
			}
			if (set[xor^1].size() == 0){
				out.println(ansx + " " + ansy);
				break;
			}
		}
	}
	public void add(int x, int y){
		if (!( x >= 0 && y >= 0 && x < n && y < m && !used[x][y])) return;
		set[xor^1].add(10000L*x + y);
		used[x][y] = true;
	}

}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.io.*;
import java.util.*;

public class Main {

    static BufferedReader br;
    static PrintWriter out;
    static StringTokenizer st;
    
    static int[][] moves = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
    
    static boolean correct(int x, int y, int n, int m) {
        return (x >= 0 && x < n && y >= 0 && y < m); 
    }
    
    static void solve() throws Exception {
        int n = nextInt();
        int m = nextInt();
        int k = nextInt();
        int[][] order = new int[n][m];
        boolean[][] used = new boolean[n][m];
        Queue<Integer[]> q = new LinkedList<>();
        Set<String> set = new HashSet<String>();
        for(int i = 0; i < k; i++) {
            int x = nextInt() - 1;
            int y = nextInt() - 1;
            order[x][y] = 1;
            used[x][y] = true;
            q.add(new Integer[] {x, y});
            set.add(x + "" + y);
        }
        while(!q.isEmpty()) {
            Integer[] v = q.remove();
            for(int[] move : moves) {
                int x = v[0] + move[0];
                int y = v[1] + move[1];
//              if(set.contains(x + "" + y)) {
//                  continue;
//              }
                if(correct(x, y, n, m) && !used[x][y]) {
                    q.add(new Integer[] {x, y});
                    used[x][y] = true;
                    order[x][y] = order[v[0]][v[1]] + 1; 
                }
            }
        }
        int max = Integer.MIN_VALUE;
        int maxI = -1;
        int maxJ = -1;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(order[i][j] > max) {
                    max = order[i][j];
                    maxI = i;
                    maxJ = j;
                }
            }
        }
        maxI++;
        maxJ++;
        out.println(maxI + " " + maxJ);
    }
    
 
    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            String line = br.readLine();
            if (line == null) {
                return null;
            }
            st = new StringTokenizer(line);
        }
        return st.nextToken();
    }

    public static void main(String[] args) {
        try {
            InputStream input = System.in;
            OutputStream output = System.out;
            br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
            out = new PrintWriter(new PrintStream(new File("output.txt")));
            solve();
            out.close();
            br.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
// Problem: 35_C. Fire Again
// Time complexity: cubic

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Main {
	public static int n,m;
	public static void main(String[] arg) {
		FastScanner scan = null;
		PrintWriter out = null;
		try{
			scan = new FastScanner(new FileInputStream("input.txt"));
			out = new PrintWriter(new FileOutputStream("output.txt"));
		}catch(FileNotFoundException e){
			scan = new FastScanner(System.in);
			out = new PrintWriter(System.out);
		}
		
		 n = scan.nextInt();
		 m = scan.nextInt();
		int k = scan.nextInt();
		int[][] board = new int[n+1][m+1];
		String[] ins = scan.nextLine().split(" ",-1);
		List<Integer> ps = new ArrayList<Integer>();
		for(int i = 0; i < 2 * k; i += 2){
			int a = Integer.parseInt(ins[i]);
			int b = Integer.parseInt(ins[i+1]);
			board[a][b] = 1;
			ps.add(a * 2001 + b);
		}
		
		int retx = 1, rety = 1;
		int[] dx = {0,1,0,-1};
		int[] dy = {1,0,-1,0};
		while(true){
			boolean find = false;
			List<Integer> ps2 = new ArrayList<Integer>();
			for(Integer p : ps){
				int i = p / 2001;
				int j = p % 2001;
				for(int q = 0; q < 4; q++){
					int nx = i + dx[q];
					int ny = j + dy[q];
					if(in(nx,ny) && board[nx][ny] == 0){
						board[nx][ny] = 1;
						retx = nx;
						rety = ny;
						find = true;
						ps2.add(nx * 2001 + ny);
					}
				}
				board[i][j] = 2;
			}
			ps = ps2;
			if(!find) break;
		}
		out.println(retx + " " + rety);
		out.close();
	}
	public static boolean in(int i, int j){
		return (1 <= i && i <= n) && (1 <= j && j <= m);
	}
	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;
 
		FastScanner(InputStream is) {
			try {
				br = new BufferedReader(new InputStreamReader(is));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
 
		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					return null;
				}
			}
			return st.nextToken();
		}
	    
		String nextLine() {
	        try {
	            return br.readLine();
	        }
	        catch (Exception e) {
	            return null;
	        }
	    }
		int nextInt() {
			return Integer.parseInt(next());
		}
 
		long nextLong() {
			return Long.parseLong(next());
		}
 
		double nextDouble() {
			return Double.valueOf(next());
		}
	}
}
// Problem: 35_C. Fire Again
// Time complexity: cubic


import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

/**
 * Created by mostafa on 10/7/17.
 */
public class FireAgain {
    static int n, m, k;
    static int inf = (int) 1e9;
    static class Pair {
        int x, y;
        Pair(int a, int b) {
            x = a; y = b;
        }
    }
    static int[] dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
    static boolean valid(int x, int y) {
        return x >= 0 && x < n && y >= 0 && y < m;
    }
    static int[][] bfs(int[] xs, int[] ys) {
        int[][] dist = new int[n][m];
        for(int i = 0; i < n; i++)
            Arrays.fill(dist[i], inf);
        Queue<Pair> q = new LinkedList<>();
        for(int i = 0; i < k; i++) {
            dist[xs[i]][ys[i]] = 0;
            q.add(new Pair(xs[i], ys[i]));
        }

        while(!q.isEmpty()) {
            Pair p = q.remove();
            for(int d = 0; d < 4; d++) {
                int nx = p.x + dx[d], ny = p.y + dy[d];
                if(valid(nx, ny) && dist[nx][ny] == inf) {
                    dist[nx][ny] = dist[p.x][p.y] + 1;
                    q.add(new Pair(nx, ny));
                }
            }
        }

        return dist;
    }

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner();
        n = sc.nextInt(); m = sc.nextInt(); k = sc.nextInt();
        int[] xs = new int[k], ys = new int[k];
        for(int i = 0; i < k; i++) {
            xs[i] = sc.nextInt() - 1; ys[i] = sc.nextInt() - 1;
        }

        int[][] dist = bfs(xs, ys);

        int x = 0, y = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(dist[i][j] > dist[x][y]) {
                    x = i; y = j;
                }
        x++; y++;
        PrintWriter out = new PrintWriter("output.txt");
        out.println(x + " " + y);
        out.flush();
        out.close();
    }

    static class Scanner {
        BufferedReader br;
        StringTokenizer st;
        Scanner() throws FileNotFoundException {
            br = new BufferedReader(new FileReader("input.txt"));
        }

        String next() throws IOException {
            while(st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
    }
}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class Contest35_3 {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("input.txt"));
        String[] s = in.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);
        int k = Integer.parseInt(in.readLine());
        s = in.readLine().split(" ");
        Point[] inp = new Point[k];
        int p = 0;
        for (int i = 0; i < k; i++) {
            inp[i] = new Point(Integer.parseInt(s[p++]),
                    Integer.parseInt(s[p++]));
        }
        int max = -1;
        int maxx = -1;
        int maxy = -1;
        int i;
        int j, dist;
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= m; j++) {
                dist = 1000000;
                for (int l = 0; l < inp.length; l++) {
                    dist = Math.min(
                            Math.abs(inp[l].x - i) + Math.abs(inp[l].y - j),
                            dist);
                }
                if (dist > max) {
                    max = dist;
                    maxx = i;
                    maxy = j;
                }
            }
        }
        String res = maxx + " " + maxy + "\n";
        FileWriter out = new FileWriter(new File("output.txt"));
        out.append(res);
        out.flush();
        out.close();
    }
}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.io.*;
import java.util.*;

import static java.lang.Math.*;

public class incendio {
    void dbg(Object...os) { System.err.println(Arrays.deepToString(os)); }
    static StringTokenizer _stk; static BufferedReader input; static PrintWriter output; 
    static String next(){return _stk.nextToken();} static int nextInt(){return Integer.parseInt(next());}
    static String readln()throws IOException {String l=input.readLine();_stk=l==null?null:new StringTokenizer(l," ");return l;}
    public static void main(String[] args) throws IOException {
        input = new BufferedReader(new FileReader("input.txt")); 
        output = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
        new incendio();
        output.close();
    }

    
    incendio() throws IOException {
        readln();
        M = nextInt(); N = nextInt();
        readln();
        final int K = nextInt();
        int xf[]=new int[K], yf[]=new int[K];
        readln();
        for(int i=0; i<K; i++) {
            xf[i]=nextInt();
            yf[i]=nextInt();
        }
        
        int best=-1, xbest=0, ybest=0;
        for(int i=1; i<=M; i++) {
            for(int j=1; j<=N; j++) {
                int dist=Integer.MAX_VALUE;
                for(int k=0; k<K; k++) {
                    dist = Math.min(dist, Math.abs(i-xf[k])+Math.abs(j-yf[k]));
                }
                if(dist>best) {
                    best=dist;
                    xbest=i;
                    ybest=j;
                }
            }
        }
        output.println(xbest+" "+ybest);
    }
    
    int M, N;
}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.io.*;
import java.util.*;

public class Solution implements Runnable {

	Scanner input;
	PrintWriter output;
	
	private void solve() throws Exception
	{
		int n = nextInt();
		int m = nextInt();
		int k = nextInt();
		int[] r = new int[k];
		int[] c = new int[k];
		for (int i = 0; i < k; i++)
		{
			r[i] = nextInt();
			c[i] = nextInt();
		}
		int best = -1;
		int bestr = -1;
		int bestc = -1;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				int d = n + m;
				for (int q = 0; q < k; q++)
				{
					d = Math.min(d, Math.abs(i - r[q]) + Math.abs(j - c[q]));
				}
				if (d < best) continue;
				best = d;
				bestr = i;
				bestc = j;
			}
		}
		out(bestr + " " + bestc);
	}

	private int nextInt() throws Exception
	{
		return input.nextInt();
	}
	
	private void out(String s)
	{
		output.println(s);
	}

	public void run() {
		try {
			solve();
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		} finally {
			output.close();
		}
	}
	
	public Solution() throws IOException {
		input = new Scanner(new FileReader("input.txt"));
		output = new PrintWriter("output.txt");
	}

	public static void main(String[] args) throws IOException {
		Locale.setDefault(Locale.US);
		new Thread(new Solution()).start();
	}
}
// Problem: 35_C. Fire Again
// Time complexity: cubic
import java.awt.Point;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args)throws IOException, URISyntaxException {
        Reader.init(new FileInputStream("input.txt"));
        StringBuilder s=new StringBuilder();
        boolean[][]vis=new boolean[Reader.nextInt()][Reader.nextInt()];
        int k=Reader.nextInt(),r,c;
        Queue<Point>q=new LinkedList<Point>();
        while(k-->0) {
            r=Reader.nextInt()-1;
            c=Reader.nextInt()-1;
            vis[r][c]=true;
            q.add(new Point(r,c));
        }
        Point end=null;
        int[]x={0,0,1,-1},y={1,-1,0,0};
        int a,b,i;
        while(!q.isEmpty()) {
            end=q.poll();
            for(i=0;i<4;i++) {
                a=end.x+x[i];
                b=end.y+y[i];
                if(a>=0&&b>=0&&a<vis.length&&b<vis[a].length&&!vis[a][b]) {
                    vis[a][b]=true;
                    q.add(new Point(a,b));
                }
            }
        }
        s.append(end.x+1).append(' ').append(end.y+1);
        PrintWriter p=new PrintWriter("output.txt");
        p.println(s);
        p.close();
    }
}

class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) throws UnsupportedEncodingException {
        reader = new BufferedReader(
                     new InputStreamReader(input, "UTF-8") );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    
    static String nextLine() throws IOException {
        return reader.readLine();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
    
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
}
// Problem: 35_C. Fire Again
// Time complexity: cubic


import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;

public class C {
    static class Struct {
        int x, y, count;

        public Struct(int xx, int yy, int c) {
            x = xx;
            y = yy;
            count = c;
        }
    }

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(new FileReader("input.txt"));
        int n = sc.nextInt();
        int m = sc.nextInt();
        FileWriter fw=new FileWriter("output.txt");
        boolean[][] grid = new boolean[n][m];
        int[] dx = new int[] { 1, 0, -1, 0 };
        int[] dy = new int[] { 0, -1, 0, 1 };
        int k = sc.nextInt();
        LinkedList<Struct> a = new LinkedList<Struct>();
        for (int i = 0; i < k; i++) {
            a.add(new Struct(sc.nextInt() - 1, sc.nextInt() - 1, 0));
        }
        int max = Integer.MIN_VALUE, maxX = -1, maxY = -1;
        while (!a.isEmpty()) {
            Struct tmp = a.remove();
            if (grid[tmp.x][tmp.y] == true)
                continue;
            grid[tmp.x][tmp.y] = true;
            if (tmp.count > max) {
                max = tmp.count;
                maxX = tmp.x;
                maxY = tmp.y;
            }
            for (int i = 0; i < 4; i++) {
                int nx = tmp.x + dx[i];
                int ny = tmp.y + dy[i];
                if (nx < n && nx >= 0 && ny < m && ny >= 0) {
                    if (grid[nx][ny] == false) {
                        a.add(new Struct(nx, ny, tmp.count + 1));
                    }
                }
            }
        }
        fw.write((maxX + 1) + " " + (maxY + 1)+"\n");
        System.out.println((maxX + 1) + " " + (maxY + 1));
        fw.flush();
    }
}